I'm trying to extend xml.ElementTree.Element
. The problem is that the constructor gives me a ready made Element
instance which I cannot extend without tampering with the source code of xml
.
Is there a way to initialize a class that inherits from Element and copy the whole Element attributes into the SubClass?
import xml.ElementTree as ET
root = ET.parse('file.xml').getroot() # retrieves Element instance
class ExtendedElement(ET.Element):
def __init__(self, element):
pass
# somehow initialize the ExtendedElement instance
# with all methods and attributes of element
# without copying each attribute individually
# ie self.attrib = element.attrib
def custom_method(self):
print(self.attrib)
ext = ExtendedElement(root)
ext.custom_method()
assert root.attrib == ext.attrib
assert list(ext) == list(root)
Usually I would just go on and call self.__dict__.update(element.__dict__)
, however Element
doesn't seem to have a __dict__
(how is this even possible?).
I want to avoid copying all attributes individually as I might miss some hidden ones without knowing.