[Tutor] Copying a mutable

Walter Prins wprins at gmail.com
Wed Jun 8 01:11:33 CEST 2011


Hi,

2011/6/7 Válas Péter <sulinet at postafiok.hu>

> Hi,
>
> let X be a mutable container, such as dict/set/list=bytearray, and Y=X,
> When I change X, Y will follow it, having always the same value, although
> id(X)!=id(Y).


That's not correct:

Python 2.7 (r27:82525, Jul  4 2010, 07:43:08) [MSC v.1500 64 bit (AMD64)] on
win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> x=[1,2,3]
>>> y=x
>>> id(x)
36115464L
>>> id(y)
36115464L
>>> x.append(4)
>>> id(x)
36115464L
>>> id(y)
36115464L
>>> x
[1, 2, 3, 4]
>>>

As you can see, x and y are references to the same object (e.g. with the
same id.)

Regards

Walter
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110608/c5d2ba8b/attachment.html>


More information about the Tutor mailing list