Why does an Element evaluate to false?

I am using elementtree and was wondering why an Element evaluates to False:

import xml.etree.ElementTree as ET

elt = ET.Element('foo')
if elt:

I would expect “bar” to be printed because I have a valid Element, but it is not. Is there a reason for this behaviour?


An empty element (just like an empty list or dict or other container) is falsey. Add a child to it, and it will be truthy

if elt:

Outputs bar