Python 如何使用Sphinx';专用方法的Autodoc扩展?
我正在使用Sphinx来记录我的python项目。我启用了autodoc扩展,并且在我的文档中包含以下内容Python 如何使用Sphinx';专用方法的Autodoc扩展?,python,python-sphinx,autodoc,Python,Python Sphinx,Autodoc,我正在使用Sphinx来记录我的python项目。我启用了autodoc扩展,并且在我的文档中包含以下内容 .. autoclass:: ClassName :members: 问题是,它只记录类中的非私有方法。我如何也包括私有方法?您是否尝试过使用autodoc skip member来确定文档中是否应包括成员?这里有一个提示:假设私有意味着“秘密” 这就是为什么斯芬克斯不会记录他们 如果你不是说“秘密”,请考虑改变他们的名字。一般避免使用单个前导下划线名称;除非您有理由对实现保密,否
.. autoclass:: ClassName
:members:
问题是,它只记录类中的非私有方法。我如何也包括私有方法?您是否尝试过使用autodoc skip member来确定文档中是否应包括成员?这里有一个提示:假设私有意味着“秘密” 这就是为什么斯芬克斯不会记录他们
如果你不是说“秘密”,请考虑改变他们的名字。一般避免使用单个前导下划线名称;除非您有理由对实现保密,否则它不会有帮助。不,private意味着对类是私有的,不应该从公共API使用它。它并不意味着秘密,对于我们这些希望使用sphinx来完整记录类的人来说,排除私有方法是相当烦人的
前面的答案是正确的。您必须使用自定义方法,因为Sphinx目前不支持将autodoc与私有方法结合使用。解决此问题的一种方法是显式强制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
如果您正在使用sphinx 1.1或更高版本,请访问sphinx文档网站: 我们可以通过设置环境变量来更改sphinx apidoc生成的内容:
export SPHINX_APIDOC_OPTIONS='members,special-members,private-members,undoc-members,show-inheritance'
您还可以将此设置添加到Makefile中(如果您的包使用):
您可以将其添加到
conf.py
文件:
autodoc_default_flags = ['members', 'undoc-members', 'private-members', 'special-members', 'inherited-members', 'show-inheritance']
如果您只想记录特定的私有方法,而不是所有方法,则可以对每个方法使用
automethod
指令,而不是:private members:
我经常使用与普通公共方法同名的前导下划线方法,该方法具有函数的实际实现。然后,public方法对输入参数进行各种健全性检查。下划线方法跳过它们,因此可以调用它们以提高效率,但类型安全性较低
例如(向@cmcginty道歉,因为他偷了他们的例子)
然后要记录\u fly\u to
,而不是\u expose
,您可以执行以下操作:
.. autoclass:: SmokeMonster
:members:
.. automethod:: SmokeMonster._fly_to
这似乎与政治公众人物8号所说的私人问题背道而驰。“如果有疑问,请选择非公开”@svrist:不要倒退——这正是重点。斯芬克斯不会记录非公开的。如果选择非公开,则不会自动获取文档。另一方面,如果您想要文档,请不要选择非公共文档。在这里,“怀疑”意味着你有一个很好的理由,两者都不能决定。如果你没有一个非公开的好理由,你也不会有“怀疑”。除非你有充分的非公开理由,否则将其公开。但是,如果你将Sphinx用于内部文档,该怎么办?@Max:规则不会改变。“一般避免使用单个前导下划线名称”。它们不是“内部的”。它们“非常秘密,无法泄露。”如“欲了解更多信息,请阅读源代码”。像
\uuuuu add\uuuuu
,\uuuu getitem\uuuuu
这样的方法属于隐藏类别,重命名它们是不可能的。请记住。。记录私有函数
和。。automethod::_FUNC_NAME
应该放在您希望输出位于的任何位置。它们不必位于相关类的\uuuu init\uuu()
函数中。谢谢!还曾在模块级为专用模块功能工作:IE。。自动功能::_my_private_module_函数
在模块docstring和my.rst
文件中。不幸的是:私有成员:
不适用于模块级私有函数。我认为它只适用于类。请注意,您可以使用该设置将其设置为所有类的默认设置。请注意,Sphinx 1.8中不推荐使用该设置,并将其合并到autodoc\u default\u options
中,因此新版本更像:autodoc\u default\u options={“members”:True,“Unoc members”:True,“private members”:True}
autodoc_default_flags = ['members', 'undoc-members', 'private-members', 'special-members', 'inherited-members', 'show-inheritance']
class SmokeMonster(object):
"""
A large smoke monster that protects the island.
"""
def __init__(self, speed, initial_position):
"""
:param speed: Velocity in MPH of the smoke monster
:param inital_position: Position of the smoke monster
"""
self.speed = speed
self.position = initial_position
def _evaporate(self):
"""
Removes the smoke monster from reality. Not to be called by client.
"""
pass
def fly_to(self, position):
"""
Have the monster fly to the specified position.
:param position: Desired location for the monster to fly to.
"""
if not position.is_valid():
raise ValueError("Invalid position: " + str(position))
if not self.can_fly():
raise RuntimeError("Smoke monster is not currently able to fly.")
self._fly_to(position)
def _fly_to(self, position):
"""Equivalent to :meth:`SmokeMonster.fly_to`, but without the safety checks.
Not normally recommended for end users, but available if you need to
improve efficiency of the `fly_to` call and you already know it is safe
to call.
"""
self.position = position
.. autoclass:: SmokeMonster
:members:
.. automethod:: SmokeMonster._fly_to