Subclassing cElementTree.Element

Discussion in 'Python' started by Kent Johnson, Feb 7, 2005.

  1. Kent Johnson

    Kent Johnson Guest

    Is it possible to subclass cElementTree.Element? I tried
    >>> import cElementTree as et
    >>> class Elt(et.Element):

    ... 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)
    Kent Johnson, Feb 7, 2005
    #1
    1. Advertising

  2. Kent Johnson wrote:

    > Is it possible to subclass cElementTree.Element? I tried
    > >>> import cElementTree as et
    > >>> class Elt(et.Element):

    > ... 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>
    Fredrik Lundh, Feb 8, 2005
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Fredrik Lundh

    ANN: cElementTree 0.9.8 (january 23, 2005)

    Fredrik Lundh, Jan 23, 2005, in forum: Python
    Replies:
    0
    Views:
    278
    Fredrik Lundh
    Jan 23, 2005
  2. Igor V. Rafienko

    cElementTree clear semantics

    Igor V. Rafienko, Sep 25, 2005, in forum: Python
    Replies:
    27
    Views:
    656
    Paul Boddie
    Sep 26, 2005
  3. Diez B. Roggisch

    cElementTree encoding woes

    Diez B. Roggisch, Feb 20, 2006, in forum: Python
    Replies:
    3
    Views:
    395
    Fredrik Lundh
    Feb 20, 2006
  4. Mark
    Replies:
    0
    Views:
    310
  5. mukappa
    Replies:
    2
    Views:
    462
    mukappa
    Jan 25, 2007
Loading...

Share This Page