如何使用Sphinx的自动文档来记录类的__init __(self)方法? [英] How to use Sphinx's autodoc to document a class's __init__(self) method?

查看:86
本文介绍了如何使用Sphinx的自动文档来记录类的__init __(self)方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Sphinx默认情况下不会为__init __(self)生成文档。我尝试了以下方法:

Sphinx doesn't generate docs for __init__(self) by default. I have tried the following:

.. automodule:: mymodule
    :members:

..autoclass:: MyClass
    :members:

在conf.py中,设置以下内容只会附加__init __(self)docstring到类docstring( Sphinx autodoc文档似乎同意这一点是预期的行为,但未提及我要解决的问题):

In conf.py, setting the following only appends the __init__(self) docstring to the class docstring (the Sphinx autodoc documentation seems to agree that this is the expected behavior, but mentions nothing regarding the problem I'm trying to solve):

autoclass_content = 'both'


推荐答案

以下是三种选择:


  1. 要确保始终记录 __ init __(),可以使用 autodoc-skip-member 。像这样:

  1. To ensure that __init__() is always documented, you can use autodoc-skip-member in conf.py. Like this:

def skip(app, what, name, obj, would_skip, options):
    if name == "__init__":
        return False
    return would_skip

def setup(app):
    app.connect("autodoc-skip-member", skip)

这明确定义了 __ init __ 不被跳过(默认情况下)。此配置仅指定一次,并且不需要为.rst源中的每个类添加任何额外的标记。

This explicitly defines __init__ not to be skipped (which it is by default). This configuration is specified once, and it does not require any additional markup for every class in the .rst source.

特殊成员 选项为<在Sphinx 1.1中添加了href = http://sphinx.pocoo.org/changes.html#release-1-1-oct-9-2011 rel = noreferrer> 。它使特殊成员(名称如 __ special __ 的成员)由autodoc记录。

The special-members option was added in Sphinx 1.1. It makes "special" members (those with names like __special__) be documented by autodoc.

自Sphinx 1.2起,

Since Sphinx 1.2, this option takes arguments which makes it more useful than it was previously.

使用自动方法

.. autoclass:: MyClass     
   :members: 

   .. automethod:: __init__

必须为每个类添加此名称(不能与 automodule一起使用,如对此答案的第一版的注释中指出的那样。)

This has to be added for every class (cannot be used with automodule, as pointed out in a comment to the first revision of this answer).

这篇关于如何使用Sphinx的自动文档来记录类的__init __(self)方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆