I have a Python code whose structure is as following:
for i in range (0,N):
ClassInstance = A(params)
... #some operation that modify the attribute of the instance "ClassInstance" of the class "A"
A
is a class linked to another one by a class composition relation.
Now I want to reset at each loop cycle the class' instance and:
- I don't want to create a new instance for each cycle with a different name
- I don't want to write a method in
A
for the manual reset of attributes since they are many and not all defined in the__init__
method but spreaded inside the various methods of the class.
I just want that at each cycle the same old instance assumes the same state that had just after its creation; to do so I'd proceed putting a reset method in A
like the following:
def reset(self, params):
self = A(params)
and modify the structure of the code as follow:
ClassInstance = A(params)
for i in range (0,N):
ClassInstance.reset(params)
... #some operation that modify the attribute of the instance "ClassInstance" of the class "A"
Is it a safe way to lose track of the previous history of ClassInstance, restarting at each cycle from 0, or there is some cons that I'm not considering?
P.S. Searching online I saw some previous similar post (as Preferred way of resetting a class in Python) but I'd like to understand if this specific way works and if I should be careful about something when proceeding by it. Clearly if this method is a wrong way to solve my problem, other approches/solutions that fit my circumstance are well accepted as well (but in that case I'd like to understand where is the problem)