As by the following example, the id of the deep-copied objects changes, but not if deepcopy is applied to an object of objects. Why?
import copy
import numpy
class A:
data = numpy.array([])
def __init__(self, numbers):
self.data = numpy.array(numbers)
class B:
As = []
def __init__(self,*many_A):
for a in many_A:
self.As.append(a)
A1 = A([1,2,3])
print(["A1:",id(A1.data)]) # OK
A2 = A([4,5,6])
print(["A2:",id(A2.data)]) # Also OK
A3 = copy.deepcopy(A2)
print(["A3:",id(A3.data)]) # Ok, deepcopy changes id!
B1 = B(A1,A2)
print(["B1.A[0]:",id(B1.As[0].data)]) # ok, same id as A1
print(["B1.A[1]:",id(B1.As[1].data)]) # ok, same id as A2
B2 = copy.deepcopy(B1)
print(["B2.A[0]:",id(B2.As[0].data)]) # wtf???? why does deepcopy not change id's here?
print(["B2.A[1]:",id(B2.As[1].data)]) # wtf????
The output of the code:
['A1:', 1557465682928]
['A2:', 1557465683120]
['A3:', 1557465682832]
['B1.A[0]:', 1557465682928]
['B1.A[1]:', 1557465683120]
['B2.A[0]:', 1557465682928]
['B2.A[1]:', 1557465683120]
This is very strange behaviour. Deepcopy knows how to deepcopy A, but not B, which only consists of a list of A's.