[Python-Dev] Doc suggestion for Elementtree (for 2.5? a bit late, I know...)

Paul Moore p.f.moore at gmail.com
Mon Aug 28 12:18:38 CEST 2006


One little addition to the elementtree docs. In the overview section,
adding a paragraph explaining best practice for importing the module
might be useful.

Some suggested text, for the "overview" section:

"""
The ElementTree module comes in two forms - a pure-python version
(xml.etree.ElementTree) and a C-coded implementation
(xml.etree.cElementTree) which is faster. To import the faster code if
possible, but fall back to the Python implementation, you can use

try:
    from xml.etree import cElementTree as ET
except ImportError:
    from xml.etree import ElementTree as ET

ElementTree is also available as an external module for older Python
versions. For portability to these versions, this pattern can be
extended to

try:
    from xml.etree import cElementTree as ET
except ImportError:
    try:
        from xml.etree import ElementTree as ET
    except ImportError:
        try:
            import cElementTree as ET
        except ImportError:
            import ElementTree as ET

"""

I'd put a patch on SF, but guess what? It's down again :-(

Paul.

PS This actually begs the question - are there platforms where
xml.etree.cElementTree is not available? If not, is there a need for
both versions? If there are, the wording above should probably be
modified to reflect this.


More information about the Python-Dev mailing list