Suppose I have a class:
import copy
class TestClass:
def __init__(self, value):
if isinstance(value, TestClass):
self = value.deepcopy()
else:
self.data = value
def deepcopy(self):
return copy.deepcopy(self)
where I want to write the code such that if an instance of a class is initialized by another instance of the same class, it will make a deepcopy
of the second class.
Now, if I try
In []: x = TestClass(3)
In []: x.data
Out[]: 3
But if I try
In []: y = TestClass(x)
Out[]: y.data
...
AttributeError: 'TestClass' object has no attribute 'data'
Why didn't the deepcopy
happen when the instance x
was passed to y
?