I am writing a program which involves recursively making an instance of an object that may be passed as an argument. A sample of program:
from copy import copy
class test():
def __init__(self, sample=None):
if not sample:
self.a = int(input())
self.b = int(input())
else:
self = copy(sample)
# MAIN HERE..
sampleobj1 = test()
print (sampleobj1.a, sampleobj1.b)
sampleobj2 = test(sampleobj1)
print (sampleobj2.a, sampleobj2.b)
How do I clone an object (here sampleobj1) instead of manually assigning all variables of "sample" to self? I get the following error:
Traceback (most recent call last):
File "test.py", line 17, in <module>
print (sampleobj2.a, sampleobj2.b)
AttributeError: 'test' object has no attribute 'a'
Why doesn't the line: self = sample work? Whatever I do, I always happen to get the same error. Individually copying the attributes seem just fine. But I am working on a code with a lot of attributes where copying each attribute seems a bit lengthy.
sampleobj3 = copy(sampleobj1) also seems to work. But I want the copying to be done in the class & not in the main of the program.