22

When creating a C extension to Python, is it possible to be able to somehow write comments that are exposed as docstrings to users of the extension?

1
  • You may already be aware of it, but if not: You might want to investigate Cython. You write Python-style code, add some type declarations, and it gets translated to C and compiled. Commented Jun 6, 2011 at 23:58

1 Answer 1

21

Docstrings for types can be included as the tp_doc member in the PyTypeObject structure, see an example in the docs.

Docstrings for functions can be included in the ml_doc field of the module's method table. If you want your docstrings to be "physically close" to the actual functions, you could include string constants above your function definitions which you reference in the method table.

Docstrings for methods can be assigned to the doc field in the type's member table.

Docstrings for modules can be passed as a parameter to the Py_InitModule3() or Py_InitModule4() functions.


UPDATE: Python3 does not support Py_InitModule3(), and the method has been replaced with PyModule_Create().

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

Comments

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.