【发布时间】:2014-01-08 12:24:31
【问题描述】:
我不明白为什么在 Python 3 中我不能向 ElementTree.Element 实例添加一些属性。区别如下:
在 Python 2 中:
Python 2.6.6 (r266:84292, Jun 18 2012, 14:18:47)
[GCC 4.4.6 20110731 (Red Hat 4.4.6-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from xml.etree import ElementTree as ET
>>> el = ET.Element('table')
>>> el.foo = 50
>>> el.foo
50
>>>
在 Python 3 中:
Python 3.3.0 (default, Sep 11 2013, 16:29:08)
[GCC 4.4.6 20120305 (Red Hat 4.4.6-4)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from xml.etree import ElementTree as ET
>>> el = ET.Element('table')
>>> el.foo = 50
>>> el.foo
AttributeError: foo
>>>
Python 2 由一个发行版 (CentOS) 提供。 Python 3 是从源代码编译的。
这是预期的行为、错误,还是我必须使用一些额外的标志重新编译 python 3?
更新:
一些澄清:我正在尝试在 Python 对象上设置属性,即在 Element 实例上。不是 XML 属性 (Element.attrib)。
这个问题实际上是在我尝试继承 Element 时出现的。这是一个例子:
>>> class Table(ET.Element):
... def __init__(self):
... super().__init__('table')
... print('calling __init__')
... self.foo = 50
...
>>> t = Table()
calling __init__
>>> t.foo
Traceback (most recent call last):
File "<input>", line 1, in <module>
AttributeError: 'Table' object has no attribute 'foo'
>>>
这让我觉得Element 类以某种棘手的方式实例化,但我不知道发生了什么。于是就有了这个问题。
【问题讨论】:
-
我没有使用 python 3(仍在使用 2),但您可以尝试转储 t.__dict__ 并查看 foo 是否在其中?
-
再次:请参阅我的答案中的第一个链接,并请检查返回的 python 3 对象是否具有
__dict__属性(并且可能等效地,如果它具有__slots__属性)。使用__slots__会带来更好的内存性能,但有副作用,即您无法以正常方式添加属性(并且由于 etree [曾经] 是内存猪,这正是我希望他们做的) .如果它确实有槽,你只需要在你的对象中声明它们,为你想要的新字段声明一个__slots__,然后它就可以工作了。 (如果这是问题..)
标签: python elementtree