Augument assignment versus regular assignment

nagy nakshantala at gmail.com
Fri Jul 7 23:14:14 EDT 2006


I do the following. First create lists x,y,z. Then add an element to x
using the augumented assignment operator. This causes all the other
lists to be changed also.
But if I use the assignment x=x+[4] instead of using the augumented
assignment, the y and z lists do not change.
Why is that?
This does not happen when I work with integer data type for example, as
shown below.

Thanks for your help
Nagy

>>> x=y=z=[]
>>> x+=[2]
>>> x
[2]
>>> y
[2]
>>> z
[2]
>>> x=x+[4]
>>>
>>> x
[2, 4]
>>> y
[2]
>>> z
[2]
>>> a=b=4
>>> b
4
>>> a+=2
>>> a
6
>>> b
4




More information about the Python-list mailing list