TypeError when subclassing 'list'

G

Gerard Flanagan

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()
 
S

Steve Juranich

Gerard said:
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. ....
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

I haven't put a lot of study into what you've done, but right off the bat,
your use of "super" is incorrect. It should look like:

super(XmlNode, self).__init__()

Not sure if that will fix your problem, though.
 
G

Gerard Flanagan

Steve said:
I haven't put a lot of study into what you've done, but right off the bat,
your use of "super" is incorrect. It should look like:

super(XmlNode, self).__init__()

Not sure if that will fix your problem, though.

Thanks Steve, but no - same error.

Gerard
 
S

Simon Percivall

The error you're seeing is because you've rebound 'list' to something
else. Try putting "list = type([])" somewhere above your code.
 
L

looping

Gerard said:
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()
#super(list, self).__init__()
I think this line must be:
super(XmlNode, self).__init__()

But just a guess...
 
G

Gerard Flanagan

Simon said:
The error you're seeing is because you've rebound 'list' to something
else. Try putting "list = type([])" somewhere above your code.

That's it! I had rebound 'list' earlier (in error), and though I
deleted the code it must have been "remembered" somehow. Restarting
PythonWin this morning, there was no error. Now I've just spent twenty
minutes wondering why 'x.apppend(y)' was raising an
AttributeException! It's going to be one of those weeks...

Thanks Simon

Gerard
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top