自动分类和实例属性 [英] autoclass and instance attributes
问题描述
根据 sphinx文档, .. autoattribute
指令应该能够记录实例属性。但是,如果我这样做了::
According to the sphinx documentation, the .. autoattribute
directive should be able to document instance attributes. However, if I do::
.. currentmodule:: xml.etree.ElementTree
.. autoclass:: ElementTree
.. autoattribute:: ElementTree._root
然后在构建时出现AttributeError:
Then when building I get an 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
并尝试访问 _r oot
属性,它可以正常工作:
even though if I instantiate ElementTree
and try and access the _root
attribute, it works fine::
>>> from xml.etree.ElementTree import ElementTree
>>> e = ElementTree()
>>> hasattr(e, '_root')
True
我在做什么错了?
(我实际上是在自己的一个类中遇到此问题,但由于它在标准库中,所以仅以ElementTree类为例)
(I'm actually having this issue with one of my own classes, but am just using the ElementTree class as an example since it's in the standard library)
推荐答案
这看起来像是处理非公共实例属性的方式的错误。 Sphinx应该能够识别在 __ init __ $ c中定义的实例属性$ c>
。
This looks like a bug in the way non-public instance attributes are handled. Sphinx is supposed to be able to recognize instance attributes defined in __init__
.
我不能说应该如何解决。有一个似乎与之相关的未解决错误报告:没有记录非公共实例属性__slots__ 。
I can't say how this should be fixed. There is an open bug report that seems to be related: Non-public instance attributes are not documented without __slots__.
如果将以下行添加到ElementTree中的 ElementTree
类的定义中。 py,
If the following line is added to the definition of the ElementTree
class in ElementTree.py,
__slots__ = ["_root"]
,那么您得到的 AttributeError
消失了。
then the AttributeError
that you get goes away.
这篇关于自动分类和实例属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!