Subtle but noticable

By Filip Salomonsson; published on May 22, 2007. Tags: python xml

http://effbot.org/zone/element.htm#attributes (emphasis mine):

Note that while the attrib value is required to be a real mutable Python dictionary, an ElementTree implementation may choose to use another internal representation, and create the dictionary only if someone asks for it.

Reality:

:::python
>>> import cElementTree as cET
>>> from lxml import etree
>>> this = cET.Element("thing", color="purple", size="huge")
>>> that = etree.Element("thing", color="purple", size="huge")
>>> type(this.attrib)
<type 'dict'>
>>> type(that.attrib)
<type 'etree._Attrib'>

Admittedly, the lxml Attrib object does support _most dictionary methods, and gains a few more in the 1.3 version (currently in beta). But you can't grab the attrib from en etree element and stuff it into a cElementTree Element, because cElementTree requires an actual dict when it's serialization time. (And doing Element(tag, **attrs) won't work either, of course.) A slight annoyance.