Update datastructures.rst#26033
Update datastructures.rst#26033dovanquyet wants to merge 1 commit intopython:3.9from dovanquyet:patch-1
Conversation
list.copy() returns a separated copy of the list, so this method should be deep copy
|
Hello, and thanks for your contribution! I'm a bot set up to make sure that the project can legally accept this contribution by verifying everyone involved has signed the PSF contributor agreement (CLA). Recognized GitHub usernameWe couldn't find a bugs.python.org (b.p.o) account corresponding to the following GitHub usernames: @Van-Quyet-DO This might be simply due to a missing "GitHub Name" entry in one's b.p.o account settings. This is necessary for legal reasons before we can look at this contribution. Please follow the steps outlined in the CPython devguide to rectify this issue. You can check yourself to see if the CLA has been received. Thanks again for the contribution, we look forward to reviewing it! |
|
I think the docs should remain as is. Consider: # Shallow Copy: just copy over the two pointers
>>> x = [["a"], ["b"]]
>>> y = x.copy()
>>> x[0] is y[0]
True
>>> x[0].append("c")
>>> x
[['a', 'c'], ['b']]
>>> y
[['a', 'c'], ['b']]
# Deep Copy: recursively copy over the contents of the two lists
>>> from copy import deepcopy
>>> x = [["a"], ["b"]]
>>> y = deepcopy(x)
>>> x[0] is y[0]
False
>>> x[0].append("c")
>>> x
[['a', 'c'], ['b']]
>>> y
[['a'], ['b']]While it's correct that list.copy() makes a copy (not just an alias like |
|
Could you create a bpo issue and associate this PR to the bpo issue. Thank you. |
|
Dear Dennis,
In the first example, x and y are actually different objects. Try
x.append('c') and show both x and y again to see the difference.
That is why I say this is a deep copy. It really depends on the data
structure of elements in x to say if those elements are shallowly or deeply
copied to y, but in terms of x and y only, the copy is deep, different from
"x = y" which I call shallow copy.
Regards,
Quyet.
…On Tue, May 11, 2021 at 11:26 PM Dennis Sweeney ***@***.***> wrote:
I think the docs should remain as is. Consider:
# Shallow Copy: just copy over the two pointers>>> x = [["a"], ["b"]]>>> y = x.copy()>>> x[0] is y[0]True>>> x[0].append("c")>>> x
[['a', 'c'], ['b']]>>> y
[['a', 'c'], ['b']]
# Deep Copy: recursively copy over the contents of the two lists>>> from copy import deepcopy>>> x = [["a"], ["b"]]>>> y = deepcopy(x)>>> x[0] is y[0]False>>> x[0].append("c")>>> x
[['a', 'c'], ['b']]>>> y
[['a'], ['b']]
While it's correct that list.copy() makes a copy (not just an alias like y
= x), it is not a deep copy, since the *contents* are just aliased.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#26033 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/ALINIJ5FKK7XZX5EU25COETTNFEDNANCNFSM44VLKCUQ>
.
--
*********************************
Đỗ Văn Quyết
Mobile: +84-(0) 982 910 568
Email: ***@***.***
|
|
I may be wrong but to me
Deep copy is when you recursively copy all contents of an object, not just the references. You can use python-tutor to visualize that: |
|
Oh I see. Thank you.
…On May 15, 2021 16:32, "Miguel Brito" ***@***.***> wrote:
I may be wrong but to me x = y is not synonymous to object contents copy
but reference copy only.
list.copy indeed returns a different object, that is, a new list but the
copy is not deep. You have a new list but each position points to the same
object. The fact that the lists are different doesn't mean they were deeply
copied.
Deep copy is when you recursively copy all contents of an object, not just
the references.
You can use python-tutor
<http://pythontutor.com/visualize.html#code=x%20%3D%20%5B%5B%22a%22%5D,%20%5B%22b%22%5D%5D%0Ay%20%3D%20x.copy%28%29%0Ax%5B0%5D.append%28%22c%22%29%0Ax%0Ay&cumulative=false&curInstr=5&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false>
to visualize that:
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#26033 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/ALINIJZG25JP65KOOMJH5LLTNY5SFANCNFSM44VLKCUQ>
.
|
|
As discussed, the proposed change is incorrect. |
list.copy() returns a separated copy of the list, so this method should be deep copy