Subclassing cElementTree.Element

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Kent Johnson

    #1

    Subclassing cElementTree.Element

    Is it possible to subclass cElementTree.El ement? I tried[color=blue][color=green][color=darkred]
    >>> import cElementTree as et
    >>> class Elt(et.Element) :[/color][/color][/color]
    ... pass
    ...
    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    TypeError: Error when calling the metaclass bases
    cannot create 'builtin_functi on_or_method' instances

    I want to create a tree where I can navigate from a node to its parent. The standard Element class
    doesn't seem to support this so I am trying to make a subclass that does. The XML files in question
    are large so the speed of cElementTree is very helpful.

    Thanks,
    Kent

    (apologies if this is a duplicate post)
  • Fredrik Lundh

    #2
    Re: Subclassing cElementTree.El ement

    Kent Johnson wrote:
    [color=blue]
    > Is it possible to subclass cElementTree.El ement? I tried[color=green][color=darkred]
    > >>> import cElementTree as et
    > >>> class Elt(et.Element) :[/color][/color]
    > ... pass
    > ...
    > Traceback (most recent call last):
    > File "<stdin>", line 1, in ?
    > TypeError: Error when calling the metaclass bases
    > cannot create 'builtin_functi on_or_method' instances[/color]

    Nope.

    This is by design; Elements are intended to be contained, not subclassed. (you
    cannot subclass ElementTree Elements either; Element is a factory function, not
    a class).

    To solve the navigation issue, you could either use a dictionary that keeps track
    of child-parent relations, or use a proxy wrapper. Or see if you can structure your
    code so you always work from parents (that's usually easier than you may think).

    </F>



    Comment

    Working...