Extending functionality of Element and ElementTree
<p>I would like to extend the functionality of <code>Element</code> and <code>ElementTree</code> classes from <code>xml.etree</code> and use them with <code>xml.etree.ElementTree.parse()</code>.</p>

<p>After a few tries, I've managed to create a solution for that problem, but I would like to know if there is a better solution or if this one has any hidden dangers.</p>

<p><strong>FooElementTree.py</strong></p>

<pre><code>import xml.etree
from xml.etree.ElementTree import Element
from xml.etree.ElementTree import ElementTree

class FooElement(Element):
    def __repr__(self):
        return "&lt;FooElement %s at 0x%x&gt;" % (repr(self.tag), id(self))

class FooElementTree(ElementTree):
    pass

xml.etree.ElementTree.Element = FooElement
xml.etree.ElementTree.ElementTree = FooElementTree
from xml.etree.ElementTree import parse
</code></pre>

<p><strong>Usage:</strong></p>

<pre><code>&gt;&gt;&gt; import FooElementTree
&gt;&gt;&gt; e = FooElementTree.parse('xml.cfg')
&gt;&gt;&gt; e
&lt;FooElementTree.FooElementTree object at 0x023AB650&gt;
&gt;&gt;&gt; r = e.getroot()
&gt;&gt;&gt; r
&lt;FooElement 'configuration' at 0x23c5470&gt;
</code></pre>

<p>This is the way, hot patching works. But be aware, that the patch has to be applied before any other Module (or Submodule) is imported, that uses ElementTree, too.</p>

python xml.etree