472,351 Members | 1,440 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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 1228
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Michele Simionato | last post by:
Let me show first how does it work for tuples: >>> class MyTuple(tuple): .... def __new__(cls,strng): # implicit conversion string of ints =>...
8
by: JustSomeGuy | last post by:
I need to write an new class derived from the list class. This class stores data in the list to the disk if an object that is added to the list is...
2
by: '[] WiRaN | last post by:
I all, I need a classroom equivalent the "Class List" of c++, necessary to recoup given of some hands tanks, Bruno
10
by: Andrew McLellan | last post by:
I think I must be missing something about generics, perhaps just about the syntax. I'd like to derive a class MyList from System.Collections.Generic...
1
by: Kivak Wolf | last post by:
This is one GREAT resource made by Microsoft. http://beta.asp.net/QUICKSTART/util/classbrowser.aspx Check it out! Kivak Wolf
2
by: BBM | last post by:
I have the following base class that uses Generics in its definition. MyList(of T, C) - T is a BindingList(Of C), C is the type...
2
by: Christian Chrismann | last post by:
Hi, an application uses a wrapper class for an STL list. Basically, this template class provides the same functions (delete, append, find...) as...
15
by: jayesah | last post by:
Hi All, List and its iterator work as following way : list<intmylist; list<int>::iterator itr; itr = mylist.begin(); cout << (*itr); But...
0
by: Laurent.LAFFONT-ST | last post by:
Hi, I want to get all classes of a module in a list. I wrote this code but I wonder if there's not a simpler solution import inspect def...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....

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.