如何将Sphinx的Autodoc扩展用于私有方法?

2024-05-12 09:41:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用Sphinx来记录我的python项目。我已经启用了autodoc扩展,并且在我的文档中有以下内容。

.. autoclass:: ClassName
   :members:

问题是,它只记录类中的non-private methods。我该如何包含私有方法呢?


Tags: 项目方法文档sphinx记录privatemethodsautodoc
3条回答

如果您使用的是sphinx 1.1或更高版本,请访问sphinx文档站点http://www.sphinx-doc.org/en/master/ext/autodoc.html

:special-members:
:private-members:

您可以将此添加到conf.py文件:

autodoc_default_flags = ['members', 'undoc-members', 'private-members', 'special-members', 'inherited-members', 'show-inheritance']

解决这个问题的一种方法是显式地强制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

相关问题 更多 >