Skip to content

Comments

Update datastructures.rst#26033

Closed
dovanquyet wants to merge 1 commit intopython:3.9from
dovanquyet:patch-1
Closed

Update datastructures.rst#26033
dovanquyet wants to merge 1 commit intopython:3.9from
dovanquyet:patch-1

Conversation

@dovanquyet
Copy link

list.copy() returns a separated copy of the list, so this method should be deep copy

list.copy() returns a separated copy of the list, so this method should be deep copy
@the-knights-who-say-ni
Copy link

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 username

We 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!

@bedevere-bot bedevere-bot added the docs Documentation in the Doc dir label May 11, 2021
@sweeneyde
Copy link
Member

sweeneyde commented May 11, 2021

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.

@matrixise
Copy link
Member

Could you create a bpo issue and associate this PR to the bpo issue. Thank you.

@dovanquyet
Copy link
Author

dovanquyet commented May 15, 2021 via email

@miguendes
Copy link
Contributor

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 to visualize that:

@dovanquyet
Copy link
Author

dovanquyet commented May 15, 2021 via email

@iritkatriel
Copy link
Member

As discussed, the proposed change is incorrect.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting changes docs Documentation in the Doc dir

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants