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.

1
  • 1
    It's been 10 years, and still no (obvious) solution to this! Commented Oct 23, 2022 at 11:22

1 Answer 1

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.

1
  • 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.
    – subhacom
    Commented Aug 19, 2012 at 10:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.