Execute code in autodoc docstring

2019-09-14 06:02发布

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条回答
时光不老,我们不散
2楼-- · 2019-09-14 06:58

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

查看更多
登录 后发表回答