On 07/01/2022 04:22, GC Toph wrote:

In python 3.10.1 documentation(The Python Tutorial -> An Informal introduction to Python -> Lists)



Maybe it should be replaced by “deep copy” ?


Here is my try:

Since integers are immutable (and not containers), what you have demonstrated is a shallow copy - in fact a deep and shallow copy are indistinguishable if the container being copied doesn't also contain other containers.

>>> a = [0,1, {2,3}]
>>> b = a [:]
>>> a is b
>>> a[2] is b[2]     # Proves this is a shallow copy - a deepcopy would have made a replica of that set (but with the same contents).
>>> b[2].add(4)
>>> a, b
([0, 1, set([2, 3, 4])], [0, 1, set([2, 3, 4])])

This shows that a slice is a shallow copy only - if it was a deepcopy then changing the set in b[2] wouldn't have also changed a[2]

docs mailing list -- docs@python.org
To unsubscribe send an email to docs-leave@python.org
Member address: anthony.flury@btinternet.com
Anthony Flury
email : anthony.flury@btinternet.com