[Tutor] Copying a mutable

Nathan nathan2012 at 163.com
Wed Jun 8 03:08:55 CEST 2011


unsubscribe


在 2011-06-08 07:11:33,"Walter Prins" <wprins at gmail.com> 写道:
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/3b301208/attachment.html>


More information about the Tutor mailing list