Maybe I don't understand the definition of shallow copy... but i'm very confused:
from the docs:
Where "s" is a list (but same question applies to dictionaries respectively).
"s.copy() | creates a shallow copy of s (same as s[:])"
Except I thought s[:]
was a deep copy. For example, see this stack overflow answer on how to copy a list (without it just pointing to the original version). And using list1.copy() seems to do a deep copy as well aka same behaviour as [:]
l1 = [1,2,3,4]
l2 = l1[:]
l3 = l1.copy()
l2.append(5)
l3[0] = 99
print(l1)
print(l2)
print(l3)
>> [1,2,3,4]
>> [1,2,3,4,5]
>> [99,2,3,4]
It would appear that l1
,l2
, and l3
are all separate objects. What am I missing?