Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify docstring for __init__ in Python C extension

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.

like image 279
subhacom Avatar asked Aug 11 '12 09:08

subhacom


1 Answers

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.

like image 83
Nathan Binkert Avatar answered Oct 31 '22 14:10

Nathan Binkert