[lxml-dev] how to inherit from ElementBase?

TP paratribulations at free.fr
Fri Feb 20 14:54:03 CET 2009


Hi everybody,

For my application, I try to inherit the ElementBase class of lxml.
I have read the following page:

http://codespeak.net/lxml/dev/api/lxml.etree.ElementBase-class.html

"Class ElementBase: The public Element class. All custom Element classes
must inherit from this one. To create an Element, use the Element()
factory."

So I have tried to define a new class deriving from ET.ElementBase, and set
the factory (__metaclass__) to ET.Element. You will find my code below. I
obtain:

$ p test_element_factory.py
Traceback (most recent call last):
  File "test_element_factory.py", line 3, in <module>
    class NewElement( ET.ElementBase ):
  File "etree.pyx", line 1844, in etree.Element
  File "apihelpers.pxi", line 129, in etree._makeElement
  File "apihelpers.pxi", line 116, in etree._makeElement
  File "etree.pyx", line 362, in etree._Document._setNodeNamespaces
  File "apihelpers.pxi", line 651, in etree._utf8
TypeError: Argument must be string or unicode.

What is the problem?
Thanks in advance,

Julien

##############################
import lxml.etree as ET

class NewElement( ET.ElementBase ):

    __metaclass__ = ET.Element

    def _init( self
            , *args
            , **kwargs ):

        super( NewElement, self ).__init__( self
                , *args
                , **kwargs )

a = NewElement( "root" )
print a
##############################


-- 
python -c "print ''.join([chr(154 - ord(c)) for c in '*9(9&(18%.\
9&1+,\'Z4(55l4('])"

"When a distinguished but elderly scientist states that something is
possible, he is almost certainly right. When he states that something is
impossible, he is very probably wrong." (first law of AC Clarke)



More information about the lxml-dev mailing list