python - 如何使用 Sphinx 的 autodoc 来记录类的 __init__(self) 方法?

标签 python python-sphinx autodoc

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

.. automodule:: mymodule
    :members:

..autoclass:: MyClass
    :members:

在 conf.py 中,设置以下内容仅将 __init__(self) 文档字符串附加到类文档字符串(the Sphinx autodoc documentation 似乎同意这是预期的行为,但没有提及我要解决的问题) :

autoclass_content = 'both'

最佳答案

这里有三种选择:

  1. 为确保始终记录 __init__(),您可以使用 autodoc-skip-member在 conf.py 中。像这样:

    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 源中的每个类添加任何附加标记。

  2. special-members选项是 added in Sphinx 1.1 .它使“特殊”成员(名称如 __special__ 的成员)由 autodoc 记录。

    自 Sphinx 1.2 起,此选项采用参数,使其比以前更有用。

  3. 使用自动方法:

    .. autoclass:: MyClass     
       :members: 
    
       .. automethod:: __init__
    

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

关于python - 如何使用 Sphinx 的 autodoc 来记录类的 __init__(self) 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5599254/

相关文章:

python - 在 results() 对象中显示所有命中

python - 接受一个列表并返回一个仅包含原始元素的列表的函数?

c - 使用相同的代码为 2 种语言生成文档

python - 狮身人面像自动模块 : how to reference classes in same module?

python - Django 应用程序未在 Elastic Beanstalk AWS 上启动

python - 我想从数据框中的行创建值列表,并删除前 2 个元素

python-sphinx - 我在阅读文档上构建项目时遇到问题

latex - 对于 Sphinx,我需要对 "register" 'latexpdf' 做什么?

python - Sphinx 不处理 python 类

python - 从 sphinx autodoc 发出 reStructuredText?