TypeError when subclassing 'list'

Gerard Flanagan grflanagan at yahoo.co.uk
Sun Feb 26 17:15:22 EST 2006


Hello all

Could anyone shed any light on the following Exception? The code which
caused it is below.  Uncommenting the 'super' call in 'XmlNode' gives
the same error. If I make XmlNode a subclass of 'object' rather than
'list' then the code will run.

Thanks in advance.

Exception:

Traceback (most recent call last):
  File "C:\Program
Files\Python\Python24\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py",
line 310, in RunScript
    exec codeObject in __main__.__dict__
  File "C:\Documents and Settings\Gerard\My
Documents\Scripts\Python\XmlNode\XmlNode.py", line 5, in ?
    class XmlNode(list):
TypeError: Error when calling the metaclass bases
    __init__() takes at most 2 arguments (4 given)

Code:

from elementtree.SimpleXMLWriter import XMLWriter

class XmlNode(list):
    tag = None
    attrib = None
    value = None
    def __init__(self, tag, **attrib):
        #super(list, self).__init__()
        self.tag = tag
        self.attrib = attrib

    def __repr__(self):
        return "<XmlNode %s at %x>" % (self.tag, id(self))

    def write(self, writer):
        writer.start(self.tag, self.attrib)
        if self.value is not None:
            writer.data(self.value)
##        for node in self:
##            node.write(writer)
        writer.end()

class HtmlElement(XmlNode):
    def __init__(self, tag, value='', **attrib):
        super(HtmlElement, self).__init__(tag=tag, **attrib)
        self.value = value

class li(HtmlElement):
    def __init__(self, value=None, **attrib):
        super(li, self).__init__(tag='li', **attrib)

class ul(HtmlElement):
    def __init__(self, **attrib):
        super(ul, self).__init__(tag='ul', **attrib)

if __name__ == '__main__':
    from StringIO import StringIO

    item = li('item')
    items = ul()
    #items.apppend(item)
    out = StringIO()
    writer = XMLWriter(out)
    items.write(writer)
    print
    print out.getvalue()
    out.close()




More information about the Python-list mailing list