How to specify docstring for __init__ in Python C extension
Asked Answered
M

1

12

Perhaps a stupid question: how can one specify docstring for special functions like __init__ when writing a C extension? For ordinary methods, method table has provision for docstrings. The following autogenerated documentation is displayed when I try help(myclass):

  __init__(...)
      x.__init__(...) initializes x; see help(type(x)) for signature

But this is what I want to override.

Mafia answered 11/8, 2012 at 9:27 Comment(1)
It's been 10 years, and still no (obvious) solution to this!Darcydarda
H
1

I think that the most common thing to do is to just stick the definitions for the various functions into tp_doc and just leave it at that. You can then do as it says and look at your object's doc. This is what happens all over the standard library.

You don't really have any option of writing __doc__ on the various slots (tp_init, etc.) because they're wrapped by a wrapper_descriptor when you call PyType_Ready, and the docstring on a wrapper_descriptor is read-only.

I think that it is possible to skip using the slots and add your method (e.g. __init__) to your MemberDefs, but I've never tried that.

Hippomenes answered 19/8, 2012 at 4:58 Comment(1)
Looks like that is the case. I had already tried putting __init__ in PyMethoDef array. While it does not do any harm to put it there, the interpreter ignores the doc string I provided.Mafia

© 2022 - 2024 — McMap. All rights reserved.