本文介绍了执行autodoc docstring中的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当使用Sphinx和autodoc记录一个Python项目时,可以使用:示例:在您的module / class / function docstring中包含一个代码示例,Sphinx将会善意的语法高亮为您。这样的东西。

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
>>> 

我从Python解释器复制并粘贴了这个。有没有办法让Sphinx或autodoc执行代码并自动获取文档的输出?

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.

推荐答案

标准库包括,可以从文本中识别,执行和验证交互式示例文件或文档。

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

这篇关于执行autodoc docstring中的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 17:44