我不明白为什么在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
类是以某种棘手的方式实例化的,但我无法弄清楚是怎么回事。因此问题。
答案 0 :(得分:1)
这可能是有意的......看看Can't set attributes of object class。如果他们没有增强的ElementTree在那个时候使用插槽而不是dict,那么无论如何我都会感到惊讶。
不清楚您要做什么...您是否真的想设置 python 属性或 XML 属性?如果是后者,你真的想这样做:
el = ET.Element('table')
el.set('foo', 50)
#or
el.attrib['foo'] = 50
如果你真的想添加python属性,你应该改为子类,并且可能提供你自己的Element / SubElement函数来提供'wrapped'元素而不是标准元素。
2016年6月4日更新:也许我的回答并不清楚,但这是你可能需要做的事情(我通常是python 2.7):
class Table(ET.Element):
# Adding __slots__ gives it a known attribute to use
__slots__ = ('foo',)
def __init__(self):
super().__init__('table')
print('calling __init__')
self.foo = 50
答案 1 :(得分:1)
由于3.3 ElementTree尝试导入c实现以提高效率,但是您无法在该实现上设置任意属性。如果您每次都不想使用Set或Get方法,则应使用ET._Element_Py,这是Python实现。
答案 2 :(得分:0)
根据Python 3 documentation,似乎现在唯一的出路就是:
>>> from xml.etree import ElementTree as ET
>>> el = ET.Element('table')
>>> el.set("foo", 50)
>>> el.get("foo")
50
>>> el.attrib
{"foo": 50}
它也在Python 2.X下工作,但现在可能是强制性的。
此外,an interesting ticket in Python bug tracker他们决定更改异常消息。