List append

Stephen Obey.civ at gmail.com
Fri Sep 14 23:42:42 EDT 2007


In your code, "array" is a class attribute, so it is shared among all
instances. You need to use the __init__ method to define instance
(data) attributes instead:

def __init__(self):
    self.array = []

On Sep 14, 11:25 pm, mouseit <mouseit... at gmail.com> 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!





More information about the Python-list mailing list