autoclass和实例属性
问题描述:
根据sphinx documentation,.. autoattribute
指令应该能够记录实例属性。但是,如果我当建设做::autoclass和实例属性
.. 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类为例,因为它是在标准库)
答
这看起来像的方式中的错误不公共实例属性被处理。狮身人面像应该能够识别instance attributes defined in __init__
。
我不能说这应该如何解决。有一个开放的错误报告似乎是相关的:Non-public instance attributes are not documented without __slots__。
如果下面的行被添加到ElementTree.py的ElementTree
类的定义,
__slots__ = ["_root"]
那么你得到的AttributeError
消失。
也为我确认。任何想法,如果这是固定的,如果是这样,什么版本? – Rafe
@Rafe:错误仍然没有解决。 – mzjn