[Tutor] When is = a copy and when is it an alias

Peter Otten __peter__ at web.de
Mon Jan 27 10:54:15 CET 2014


Denis Heidtmann wrote:

> Running python 2.7 in linux
> 
> Below are two extremes.  Can I get some guidance on this?

>>>> a=zeros((2,3),dtype=int)
>>>> b=a
>>>> a[:,0]=[1,2]
>>>> a
> array([[1, 0, 0],
>        [2, 0, 0]])
>>>> b
> array([[1, 0, 0],
>        [2, 0, 0]])
>>>> a=2
>>>> a
> 2
>>>> b
> array([[1, 0, 0],
>        [2, 0, 0]])

In short:

x = ...

binds a name, it never copies. On the other side

x[whatever] = ...
or
x.attr = ...

can run arbitrary code that may or may not alter the object bound to x 
inplace. You have to know the type of x to know what happens.



More information about the Tutor mailing list