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

Denis Heidtmann denis.heidtmann at gmail.com
Tue Jan 28 20:00:49 CET 2014


On Tue, Jan 28, 2014 at 12:28 AM, spir <denis.spir at gmail.com> wrote:

> <snip>
>
>  a = [1, [1,2]]
>>>> b = a
>>>> b
>>>>
>>> [1, [1, 2]]
>
>> b is a
>>>>
>>> True
>
> a's and b's values are a single, unique object... as long as I only
> modified them (the values) partly:
>
>  a = [1,[2,3]]
>>>> a[0] = 0
>>>> b
>>>>
>>> [0, [1, 2]]                  # this is where I get lost.
>
>> a[1] = [0,0]
>>>> b
>>>>
>>> [0, [0, 0]]
>
>> a is b
>>>>
>>> True
>
>  a[0] = b[0]
>>>> a[0] is b[0]
>>>>
>>> True
> <snip>
>
> denis



My python gets a different result:

 >>> a=[1,[1,2]]
>>> b=a
>>> b
[1, [1, 2]]
>>> a=[1,[2,3]]  # this breaks the connection.
>>> a[0]=0
>>> b
[1, [1, 2]]
>>> a[1]=[0,0]
>>> b
[1, [1, 2]]
>>>

What is going on?  I am more confused than I was a week ago.

-Denis H
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20140128/8ed5f221/attachment.html>


More information about the Tutor mailing list