[Python] copy and id() function
Alice Invernizzi
invernizzi a cilea.it
Mar 13 Ott 2009 11:05:09 CEST
Dear all,
we have a doubt concerning the list copying and the id function
applied to a copied element of a list.
- shallow copy: produce a new object that is not linked to
the native object. Slicing on a list produces a shallow copy of the original one.
- deep copy: bit-bit copy (in other words we have here a
reference to the native object (link))
::::::::::::::::::::CODE:::::::::::::::::::::::::::::::::::::::::::::
#list1
l1=[1,2]
l2=[3,l1]
#shallow copy
l3=l2[:]
#deep copy: (bit-bit) or by reference
l4=l2
print 'id(l2)', id(l2), 'id(l3)', id(l3), 'id(l4)', id(l4)
print 'id(l2[1])', id(l2[1]), 'id(l3[1])', id(l3[1]), 'id(l4[1])', id(l4[1])
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
and by applying the id() function we get:
id(l2) 13069528 (ok)
id(l3) 10704976 (ok is a shallow copy)
id(l4) 13069528 (ok is a deep copy)
id(l2[1]) 13047248 (ok)
id(l3[1]) 13047248 (why is not recursive the
shallow copy?)
id(l4[1]) 13047248 (ok, is deep )
Therefore, is not clear to us why in list copy the shallow does not
affect the sub-list object. In other words the shallow copy is not
'recursive' within a shollowed copied object.
More in general, we don't understand the behavior of the id() function. Doesn't return the object identity (object adress memory)??
Why the object identity of the three variables in the following example is the same?? Does the id() function behave like the C/C++ adress-operator &??
::::::::::::::::::::::::::::::::CODE-2:::::::::::::::::::::::::::
a=23
b=23
c=a
print 'id(a):', id(a), 'id(b):', id(b), 'id(c): ',id(c)
id(a): 9985904 (ok)
id(b): 9985904 (???)
id(c): 9985904 (???)
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
Thank you very much
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.python.it/pipermail/python/attachments/20091013/b16c026a/attachment.htm
Maggiori informazioni sulla lista
Python