list.append problem?

kosh at aesaeion.com kosh at aesaeion.com
Mon Oct 1 17:55:37 EDT 2001


On 1 Oct 2001, newgene wrote:

> I am a newer of python. I have a problem like this code:
>
> >>> a=b=c=[]
> >>> a.append(1)
> >>> b.append(2)
> >>> c.append(3)
> >>> a
> [1, 2, 3]
> >>> b
> [1, 2, 3]
> >>> c
> [1, 2, 3]
>
> a,b,c become the same, although append method applied seperately.
> It's really strange and unreasonable.
> My python is ActivePython Build 210 running on WinNT.

Actually you bound a b and c to the same object. Everything in python is
by reference so you have 3 names for the same object and that is why the
append on any of them showed in all of them since they are really the
same.





More information about the Python-list mailing list