multivariable assignment

Rainer Grimm r.grimm at science-computing.de
Sat Jan 2 08:18:38 CET 2010


On Dec 31 2009, 5:13 pm, davidj411 <davidj... at gmail.com> wrote:
> I am not sure why this behavior is this way.
> at beginning of script, i want to create a bunch of empty lists and
> use each one for its own purpose.
> however, updating one list seems to update the others.
>
> >>> a = b = c = []
> >>> a.append('1')
> >>> a.append('1')
> >>> a.append('1')
> >>> c
> ['1', '1', '1']
> >>> a
> ['1', '1', '1']
> >>> b
>
> ['1', '1', '1']
a, b and c are the same objects.
You can check the object identity by
>>> a = b = c = []
>>> print id(a),id(b),id(c)
3083044140 3083044140 3083044140
>>> a is b is c
True

Greetings



More information about the Python-list mailing list