Execute code in autodoc docstring

会有一股神秘感。 提交于 2019-12-25 04:05:22

问题


When using Sphinx and autodoc to document a Python project, you can use :Example: to include a code sample in your module/class/function docstring, which Sphinx will kindly syntax highlight for you. Something like so.

>>> rng = Range(0, 1000)
>>> rng
[0, 1000)
>>> len(rng)
1000
>>> rng.start = 500
>>> rng.start
500
>>> rng.end
1000
>>> 

I copied and pasted this from the Python interpreter. Is there any way to have Sphinx or autodoc execute the code and capture the output automatically for the documentation?

My main concern is copy-and-pasted code/output getting out-of-sync with the actual current code behavior.


回答1:


The standard library includes doctest, which can identify, execute and verify interactive examples from text files or docstrings.



来源:https://stackoverflow.com/questions/41131167/execute-code-in-autodoc-docstring

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