# importing copy module
import copy
# initializing list 1
li1 = [1, 2, [3, 5], 4]
# using copy for shallow copy
li2 = copy.copy(li1)
print("Before Edit:", id(li1[0]) == id(li2[0]))
li2[0] = "hi"
print(li2, "vs", li1)
print("After Edit:", id(li1[0]) == id(li2[0]))
print(id(li1[1]) == id(li2[1]))
print(id(li1) == id(li2))
print()
# using deepcopy for deepcopy
li3 = copy.deepcopy(li1)
print("Before Edit:", id(li1[0]) == id(li3[0]))
li3[0] = "bye"
print(li3, "vs", li1)
print("After Edit:", id(li1[0]) == id(li3[0]))
print(id(li1[1]) == id(li3[1]))
print(id(li1) == id(li3))
Based on the output below, id
function in Python doesn't seem to be able to distinguish between a shallow copy and a deep copy. How can I prove that they are actually different from a memory/address standpoint?
Before Edit: True
['hi', 2, [3, 5], 4] vs [1, 2, [3, 5], 4]
After Edit: False
True
False
Before Edit: True
['bye', 2, [3, 5], 4] vs [1, 2, [3, 5], 4]
After Edit: False
True
False