Python Glossary Русский

all Python 3until 3.9Removed from the standard libraryfunction

xml.etree.ElementTree.Element.getchildren()

removed

Returned the list of direct children of an XML element. Removed in Python 3.9; use list(elem) or iterate over the element.

Syntax

elem.getchildren()  # до 3.9

An ElementTree element already behaves as a sequence of its children, so a separate method was redundant. It was deprecated back in Python 3.2 and removed in 3.9 together with getiterator(). How to rewrite: elem.getchildren() becomes list(elem); if you do not need a list, iterate directly: for child in elem. Indexing works too: elem[0], len(elem). To walk all nesting levels use elem.iter().

Examples

# Python 3.9+
import xml.etree.ElementTree as ET
root = ET.fromstring('<r><a/><b/></r>')
try:
    root.getchildren()
except AttributeError as e:
    print(type(e).__name__)  # → AttributeError
# Python 3.9+
import xml.etree.ElementTree as ET
root = ET.fromstring('<r><a/><b/></r>')
print([child.tag for child in root])  # → ['a', 'b']
print(len(root), root[0].tag)  # → 2 a
import xml.etree.ElementTree as ET
root = ET.fromstring('<r><a/><b/></r>')
print([child.tag for child in root.getchildren()])  # → ['a', 'b']

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy