I need some clarification on this concept here, I am trying out .deepcopy()
in python and I have read that:
A deep copy constructs a new compound object and then, recursively, inserts copies into it of the objects found in the original.
However, In my code below, I can see that IDs for x[0] = y[0]
and x[1] = y[1]
, but ID for x[2] != y[2]
, which makes sense because it inserts copies of the object. But why is ID of x[0]
equals y[0]
and x[1]
equals y[1]
?
This is the code that I tried:
x=[1,2,[3,4]]
y=cp.deepcopy(x)
print(id(x),id(y))
for i in range(len(x)):
print(id(x[i]),id(y[i]))
Output:
923236765248 923256513088
140729072564000 140729072564000
140729072564032 140729072564032
923256458496 923256516928