如何在Sphinx的Autodoc扩展中使用私有方法?
我正在使用Sphinx来为我的Python项目写文档。我启用了autodoc扩展,并在我的文档中有以下内容。
.. autoclass:: ClassName
:members:
问题是,它只记录了类中的非私有方法。请问我该如何把私有方法也包含进去呢?
9 个回答
10
解决这个问题的一种方法是明确告诉Sphinx去记录私有成员。你可以通过在类的文档末尾加上 automethod
来实现:
class SmokeMonster(object):
"""
A large smoke monster that protects the island.
"""
def __init__(self,speed):
"""
:param speed: Velocity in MPH of the smoke monster
:type speed: int
.. document private functions
.. automethod:: _evaporate
"""
self.speed = speed
def _evaporate(self):
"""
Removes the smoke monster from reality. Not to be called by client.
"""
pass
18
你可以把这个添加到 conf.py
文件里:
autodoc_default_options = {
"members": True,
"undoc-members": True,
"private-members": True
}
适用于 sphinx 版本 1.8 及以下
autodoc_default_flags = [
'members',
'undoc-members',
'private-members',
'special-members',
'inherited-members',
'show-inheritance'
]
1.8 版本的更新感谢 @partofthething。
36
如果你使用的是 Sphinx 1.1 或更高版本,可以从 Sphinx 的文档网站找到相关信息,网址是 http://www.sphinx-doc.org/en/master/ext/autodoc.html。
:special-members:
:private-members: