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:
  print('bar')

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

Answer

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

elt.append(ET.Element("bar"))
if elt:
    print("bar")

Outputs bar