473,378 Members | 1,384 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

TypeError when subclassing 'list'

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

Feb 26 '06 #1
5 1643
Gerard Flanagan wrote:
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.

--
Steve Juranich
Tucson, AZ
USA

Feb 26 '06 #2

Steve Juranich wrote:
Gerard Flanagan wrote:
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.

--
Steve Juranich
Tucson, AZ
USA


Thanks Steve, but no - same error.

Gerard

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

Feb 27 '06 #4
Gerard Flanagan wrote:
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...

Feb 27 '06 #5
Simon Percivall wrote:
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

Feb 27 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: GrelEns | last post by:
hello, i wonder if this possible to subclass a list or a tuple and add more attributes ? also does someone have a link to how well define is own iterable object ? what i was expecting was...
2
by: Uwe Mayer | last post by:
Hi, I want to subclass "list". The documentation states to prefer subclassing list instead of UserList. How to you clear the contents of a list subclass without creating a new object? Thanks...
7
by: ‘5ÛHH575-UAZWKVVP-7H2H48V3 | last post by:
(see end of message for example code) When an instance has a dynamically assigned instance method, deepcopy throws a TypeError with the message "TypeError: instancemethod expected at least 2...
12
by: Jane Austine | last post by:
Please see the following code: -------------------------------- class rev_wrap(object): def __init__(self,l): self.l=l def __getitem__(self,i): return self.l class rev_subclass(list): def...
7
by: Girish Sahani | last post by:
Hi, Please check out the following loop,here indexList1 and indexList2 are a list of numbers. for index1 in indexList1: for index2 in indexList2: if ti1 == ti2 and not index1 !=...
0
by: txtoth | last post by:
I'm trying to map a security_context_t ** to a python list. After calling the method that returns this type when I process the returned list in a for loop I get: TypeError: expected string or...
1
by: Freaky Chris | last post by:
This is a simple error, you are passing the variable res as an interger to use for a slice when what ever you are storing in res isn't an integer. Chris Beema shafreen wrote: -- View this...
5
by: Mike Kent | last post by:
For Python 2.5 and new-style classes, what special method is called for mylist = seq and for del mylist (given that mylist is a list, and seq is some sequence)? I'm trying to subclass list, and...
0
by: James Mills | last post by:
On Fri, Oct 31, 2008 at 8:49 AM, mark floyd <emfloyd2@gmail.comwrote: Mark, this is correct behavior. You have 3 positional arguments in the function definition. You _must_ aupply _all_ 3 of...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.