a=b change b a==b true??

Diez B. Roggisch deets at nospam.web.de
Mon Feb 26 08:58:34 EST 2007


rstupplebeen at gmail.com wrote:

> I do not have a clue what is happening in the code below.
> 
>>>> a=[[2,4],[9,3]]
>>>> b=a
>>>> [map(list.sort,b)]
> [[None, None]]
>>>> b
> [[2, 4], [3, 9]]
>>>> a
> [[2, 4], [3, 9]]
> 
> I want to make a copy of matrix a and then make changes to the
> matrices separately.  I assume that I am missing a fundamental
> concept.  Any help would be appreciated.


You are missing that the operation 

b=a

is not a copying, but a mere name-binding. The object that a points to now
is also pointed at by b. 

If you need copies, use the copy-module, with it's method deepcopy.

Diez



More information about the Python-list mailing list