List append
Rob E
remm1 at member.fsf.org
Sat Sep 15 23:05:09 EDT 2007
On Sat, 15 Sep 2007 03:25:27 +0000, mouseit wrote:
> I'm trying to add an element to a list which is a property of an
> object, stored in an array. When I append to one element, all of the
> lists are appended!
>
> Example Code:
>
> class Test:
> array = []
>
> myTests = [Test() , Test() , Test()]
> print len(myTests[1].array)
> myTests[0].array.append( 5 )
> print len(myTests[1].array)
>
> prints:
> 0
> 1
>
> This is probably a really easy question (I'm very new to python), so
> thanks in advance for your help!
Yes, that's easy:
class myclass:
var1 = []
means that var1 is associated with the class. If you want an attribute:
class myclass:
def __init__ (self):
self.var1 = []
is the correct way.
Rob
More information about the Python-list
mailing list