Docstrings in C extensions to Python?

前端 未结 1 1783
忘掉有多难
忘掉有多难 2021-02-04 03:17

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条回答
  •  孤独总比滥情好
    2021-02-04 04:01

    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().

    0 讨论(0)
提交回复
热议问题