Subclassing cElementTree.Element

K

Kent Johnson

Is it possible to subclass cElementTree.Element? I tried ... pass
...
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: Error when calling the metaclass bases
cannot create 'builtin_function_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)
 
F

Fredrik Lundh

Kent said:
Is it possible to subclass cElementTree.Element? I tried
... pass
...
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: Error when calling the metaclass bases
cannot create 'builtin_function_or_method' instances

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>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top