I define a class, in which there is a reference relationship.
Then I create an instance, after copy.deepcopy this instance, the reference relationship is gone, for example:
import numpy as np
class foo(object):
def __init__(self):
self.c = np.array([[1,2],[3,4]])
self.a = self.c[1,:]
ff0 = foo()
ff1 = copy.deepcopy(ff0)
ff1.c +=np.array([10,10])
print(ff1.a)
ff0.c +=np.array([10,10])
print(ff0.a)
Output:
[3 4]
[13 14]
But I want to see such a output:
[13 14]
[13 14]
Can anyone help me to preserve this relationship?
Thank you in advance~
I got a walk around to this problem:
import numpy as np
class foo(object):
def __init__(self):
self.c = np.array([[1,2],[3,4]])
self.a = self.c[1,:]
def copy(self):
cp = copy.deepcopy(self)
cp.a = cp.c[1,:]
return cp
ff0 = foo()
ff1 = ff0.copy()
ff1.c +=np.array([10,10])
print(ff1.a)
ff0.c +=np.array([10,10])
print(ff0.a)
Output:
[13 14]
[13 14]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.