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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With