假设我有一个名为 module.py
的模块,并且在其中有一个名为 _VAR
的私有(private)变量,我想在我的 sphinx 中打印它的值文档。
module.py
_VAR = 20
在狮身人面像中,我想要这样的东西:
index.rst
The value of ``module._VAR`` is :py:print:`module._VAR`
输出将是:
The value of module._VAR is 20
最佳答案
完全有可能通过 autodoc 做你想做的事功能。您只需要确保该值明确记录在代码中,如下所示:
_VAR = 20 #: Notice the colon, this tells sphinx to use this comment as docstring for this value
然后,您可以通过使用 include-private
执行 automodule
或明确地执行 autodata
来将此值添加到您的文档中变量名。
请注意,您可以使用相同的策略 (#:
) 显式记录类的属性,它们将出现在 autoclass
的文档中(或automodule
因为它隐式地记录类),包括它们被初始化为的任何文字值。
关于python - 在 sphinx 文档中打印变量值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24080491/