Docstrings in C extensions to Python?

大兔子大兔子 提交于 2019-12-31 12:23:12

问题


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:


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.




回答2:


I don't think so, but you should be able to manipulate the docstrings of those objects from python. See the python docs for more.



来源:https://stackoverflow.com/questions/6259114/docstrings-in-c-extensions-to-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!