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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.