简体   繁体   English

如何在 sphinx 中自动记录私有实例属性?

[英]How to autodoc private instance attributes in sphinx?

I usually initialize all instance attributes in __init__ :我通常在__init__初始化所有实例属性:

def __init__(self):
"""
Initializer docstring.
"""
    self.test1 = None  #: test1 docstring
    self._test2 = None  #: test2 docstring
    self.test3 = None
    """test3 docstring"""
    self._test4 = None
    """test4 docstring"""

I would like to have all my instance attributes documented, including the private ones.我想记录我所有的实例属性,包括私有的。

To generate the documentation I used the following code:为了生成文档,我使用了以下代码:

.. automodule:: module_name
    :members:
    :member-order: bysource
    :show-inheritance:
    :private-members:
    :special-members:

Yet private instance attributes are still omitted.然而私有实例属性仍然被省略。 What do I miss?我想念什么?

I use python 2.7, Sphinx 1.2.3, and sphinx-rtd-theme 0.1.9.我使用 python 2.7、Sphinx 1.2.3 和 sphinx-rtd-theme 0.1.9。

Thank you in advance.提前谢谢你。

I ran some additional tests and I still was not able to generate documentation for private attributes.我运行了一些额外的测试,但仍然无法为私有属性生成文档。 I do not have time to run through autodoc code to verify whether the problem is on my side or it is just a designer's choice.我没有时间运行autodoc代码来验证问题是在我这边还是只是设计师的选择。 I came up with the following work around though (in case somebody else runs in the similar problem):不过,我想出了以下解决方法(以防其他人遇到类似问题):

  1. I document all the public attributes in the class docstring (as suggested by NumPy style).我记录了类文档字符串中的所有公共属性(如NumPy风格所建议的那样)。
  2. I document all the private attributes in the __init__ docstring (as I do initialize them all in there) using the notation for variables:我使用变量的表示法在__init__文档字符串中记录所有私有属性(因为我在那里初始化它们):

     :ivar _private_attribute: description. :vartype _private_attribute: int

As a result private attributes are documented in the Variables section.因此,私有属性记录在变量部分。 It is not perfect but it works for me.它并不完美,但对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM