autoclass和实例属性

时间:2012-06-26 13:58:31

标签: python python-sphinx autodoc

根据sphinx documentation.. autoattribute指令应该能够记录实例属性。但是,如果我做::

.. currentmodule:: xml.etree.ElementTree

.. autoclass:: ElementTree

   .. autoattribute:: ElementTree._root

然后在构建时我得到一个AttributeError:

Traceback (most recent call last):etree.ElementTree.ElementTree                 
  File "/Volumes/Raptor/Library/Python/2.7/lib/python/site-packages/sphinx/ext/autodoc.py", line 326, in import_object
    obj = self.get_attr(obj, part)
  File "/Volumes/Raptor/Library/Python/2.7/lib/python/site-packages/sphinx/ext/autodoc.py", line 232, in get_attr
    return safe_getattr(obj, name, *defargs)
  File "/Volumes/Raptor/Library/Python/2.7/lib/python/site-packages/sphinx/util/inspect.py", line 70, in safe_getattr
    raise AttributeError(name)
AttributeError: _root

即使我实例化ElementTree并尝试访问_root属性,它也能正常工作::

>>> from xml.etree.ElementTree import ElementTree
>>> e = ElementTree()
>>> hasattr(e, '_root')
True

我做错了什么?

(我实际上遇到了我自己的一个类的问题,但我只是使用ElementTree类作为示例,因为它在标准库中)

1 个答案:

答案 0 :(得分:1)

这看起来像处理非公共实例属性的方式中的错误。狮身人面像应该能够识别instance attributes defined in __init__

我不知道应如何解决这个问题。有一个似乎相关的开放式错误报告:Non-public instance attributes are not documented without __slots__

如果将以下行添加到ElementTree.py中ElementTree类的定义中,

__slots__  = ["_root"]

然后你得到的AttributeError就消失了。