list in list of objects

strachon strachon at asd-software.cz
Tue Mar 4 04:52:09 EST 2003


try this (in python 2.2.2):

>>> class a:
	u = 0
	v = []
>>> x = []
>>> x.append(a())
>>> x.append(a())
>>> print x[0].u, x[0].v, x[1].u, x[1].v
returns: 0 [] 0 []

now make some changes in properties of the first object:
>>> x[0].u = 1
>>> x[0].v.append(1)
>>> print x[0].u, x[0].v, x[1].u, x[1].v
returns: 1 [1] 0 [1]

i think it is wrong, the last item should be empty
i expected something like this: 1 [1] 0 []

now append another object to the list 'x':
>>> x.append(a())
>>> print x[0].u, x[0].v, x[1].u, x[1].v, x[2].u, x[2].v
returns: 1 [1] 0 [1] 0 [1]

it is strange, the last item should be empty again


is it a bug? if not, how can i get the right behavior?




More information about the Python-list mailing list