Class warfare...
Erik Max Francis
max at alcyone.com
Fri Sep 15 12:23:18 EDT 2000
More information about the Python-list mailing list
Fri Sep 15 12:23:18 EDT 2000
- Previous message (by thread): Class warfare...
- Next message (by thread): Class warfare...
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Larry Whitley wrote: > class Bucket(): > def __init__(self): > self.cnt = 0 ... > I suspect that my [Bucket()] is producing a reference to the class > rather to > an object that is an instance of the class. You are correct. The duplication operator * for lists duplicates references, it doesn't make copies. You can see that in something like this: >>> l = [[]] # a list containing an empty list >>> print l [[]] >>> l = l * 5 # now a list of five empty lists >>> print l [[], [], [], [], []] >>> l[0].append(1) # now insert a 1 in the first contained list >>> print l # oops, they're all the same list [[1], [1], [1], [1], [1]] > >>> b = [] > >>> for i in range( 5 ): > >>> b.append( Bucket() ) > > And repeat the experiment, I no > And repeat the experiment, I now get what I expected. Is this how > it's > supposed to be done? Or is there a better way? Yep, that's the traditional way to do it. -- Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/ __ San Jose, CA, US / 37 20 N 121 53 W / ICQ16063900 / &tSftDotIotE / \ Nothing spoils a confession like repentence. \__/ Anatole France Interstelen / http://www.interstelen.com/ A multiplayer, strategic, turn-based Web game on an interstellar scale.
- Previous message (by thread): Class warfare...
- Next message (by thread): Class warfare...
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list