Unexpected boolean behavior of python worksheet elements xml.etree.ElementTree

I'm having problems using Python 2.6.5 xml.etree.ElementTree library. In particular, if I install a simple xml element like the following

>>> import xml.etree.ElementTree as etree
>>> xml = etree.fromstring("<a><b><c>xy</c></b></a>")

I have no problems with the library when accessing the internal nodes of an element, for example:

>>> etree.tostring(xml.find('b'))
'<b><c>xy</c></b>'
>>> xml.find('b') == None
False
>>> bool(xml.find('b'))
True

However, I come across a strange Boolean interpretation of leaf element nodes, see

>>> etree.tostring(xml.find('b/c'))
'<c>xy</c>'
>>> xml.find('b/c') == None
False
>>> bool(xml.find('b/c'))
False

Note that in the last command, the xml.find ('b / c') element, which is clearly not None, is set to False. This is especially annoying since I cannot use the idiom

>>> leaf = xml.find('b/c'):
>>> if leaf:
>>>     do_stuff(leaf)

to check if a leaf element exists. (I must explicitly specify "xml.find" ("b / c")! = "No."

Can someone explain this (unexpected for me) behavior?

+5

All Articles