469,903 Members | 1,610 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,903 developers. It's quick & easy.

class MyList(list): Is this ok?

Please have a look at my little program below. It works as expected
but I still feel very unsure when inheriting from builtin types.

Do I need line #1?

Is line #2 ok? Why? I came to this one more by trial and error than by
conclusion. My fingers wanted to write "self.append(v)" which creates
a nice infinite loop ...

I have read the article about "Unifying types and classes" by GvR
http://www.python.org/2.2/descrintro.html several times but probably
would need more practical examples or another tutorial.

Martin

import csv,sys

class ColumnCollector(list):

def __init__(self):
self.sums = []
list.__init__(self) #1

def append(self, v, calc=None):
list.append(self,v) #2
i = len(self)-1
try:
self.sums[i]
except IndexError:
self.sums.append(0)
if calc:
if "sum" in calc:
self.sums[i] += v

if 1 and __name__=="__main__":
print
csvw = csv.writer(sys.stdout)
cc = ColumnCollector()
for rownum in range(4):
cc.append(1,"sum")
cc.append(2,"sum")
cc.append(3,"sum")
csvw.writerow(cc)
del cc[:]
print "totals:"
csvw.writerow(cc.sums)
""" Should print:
1,2,3
1,2,3
1,2,3
1,2,3
totals:
4,8,12
"""
Jul 18 '05 #1
3 1197
On Tue, 6 Jul 2004, Martin Bless wrote:
import csv,sys

class ColumnCollector(list):

def __init__(self):
self.sums = []
list.__init__(self) #1
This is perfectly acceptable, though the new-style way to do this is with
the slightly messier line:
super(ColumnCollector,self).__init__()
def append(self, v, calc=None):
list.append(self,v) #2


Same applies here:
super(ColumnCollector,self).append(v)

What you have now though is effectively the same thing as using the
super() calls.

Jul 18 '05 #2
m.*****@gmx.de (Martin Bless) wrote in
news:40***************@news.versatel.de:
Do I need line #1?
Practically, no you don't although it is cleaner if you do. The list
initialiser would be needed if you were allowing a non-empty list to be
created.

Is line #2 ok? Why? I came to this one more by trial and error than by
conclusion. My fingers wanted to write "self.append(v)" which creates
a nice infinite loop ...

In general:

self.method(parms)

can be regarded as shorthand for:

type(self).method(self, parms)

So, if you called "self.append(v)" this would be the same as calling
"ColumnCollector.append(self, v)" which as you noticed creates an infinite
loop. You need to force the call to happen on the base class, which means
you can't use the shorthand form.

There are two ways to force a call to act on a baseclass method. The usual
way is what you came up with, just name the baseclass explicitly:

list.append(self, v)

The other way is to use the super builtin:

super(ColumnCollector, self).append(self, v)

Using super ensures that the code will continue to work if anyone starts
trying to multiply inherit from both your class and another class
inheriting from list. However, unless you wrote your class with that usage
in mind the extra complexity is not usually worthwhile (and besides,
something else would almost certainly break).
Jul 18 '05 #3
Duncan Booth <du**********@suttoncourtenay.org.uk> wrote in
<Xn***************************@127.0.0.1>:
In general:

self.method(parms)

can be regarded as shorthand for:

type(self).method(self, parms)
Aah, that's it. Thanks.
The other way is to use the super builtin:

super(ColumnCollector, self).append(self, v)


Ok, I'll have to learn more about super().

Thank you,

Martin

Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Michele Simionato | last post: by
8 posts views Thread by JustSomeGuy | last post: by
10 posts views Thread by Andrew McLellan | last post: by
1 post views Thread by Kivak Wolf | last post: by
2 posts views Thread by Christian Chrismann | last post: by
15 posts views Thread by jayesah | last post: by
reply views Thread by Laurent.LAFFONT-ST | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.