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

cnu*_*cnu 25 python documentation python-sphinx

我正在使用Sphinx来记录我的python项目.我启用了autodoc扩展,并在我的文档中包含以下内容.

.. autoclass:: ClassName
   :members:
Run Code Online (Sandbox Code Playgroud)

问题是,它只记录了类中的非私有方法.我如何包含私有方法?

mar*_*omo 27

若您使用的是sphinx 1.1或以上,请访问sphinx文档网站http://www.sphinx-doc.org/en/master/ext/autodoc.html,

:special-members:
:private-members:
Run Code Online (Sandbox Code Playgroud)

  • 请注意,您可以使用[`autodoc_default_flags`](http://sphinx.pocoo.org/ext/autodoc.html#confval-autodoc_default_flags)设置将其设置为所有类的默认值. (6认同)

cmc*_*nty 8

解决这个问题的一种方法是明确强制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
Run Code Online (Sandbox Code Playgroud)


小智 8

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

autodoc_default_flags = ['members', 'undoc-members', 'private-members', 'special-members', 'inherited-members', 'show-inheritance']
Run Code Online (Sandbox Code Playgroud)

  • 注意,它在 Sphinx 1.8 中已被弃用并合并到 `autodoc_default_options` 中,因此新版本看起来更像是: `autodoc_default_options = { "members": True, "undoc-members": True, "private-members": True } ` https://www.sphinx-doc.org/en/master/usage/extensions/autodoc.html#confval-autodoc_default_options (3认同)

Vin*_*jip 4

您是否尝试过使用自定义方法来确定是否应将成员包含在文档中,使用autodoc-skip-member