Home > Software engineering >  Factory Reset of a class instance
Factory Reset of a class instance

Time:10-26

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)

CodePudding user response:

You have to understand what does the = operator when you assign to a variable like in a=obj

That does not modify the object which the variable was pointing at. That just set which object the variable will be referencing from then on, leaving the old object alone.

So ClassInstance = A() makes a new object and assign it to the variable ClassInstance

The old instance is not referenced anymore, and you cannot access it anymore. So in each loop you have a new fresh instance.

Also, you cannot reset an instance by doing self = A() You are assigning a new object to the local variable self. The instance which was referenced by self is not touched. Moreover, when the method returns, the self variable is released and the instance is not referenced anymore, so it is destroyed.

In general assigning to variables does not do anything. Is just like putting a label to an object. self is a variable like any other. There is nothing special about it.

The only way of resetting an instance is by manually setting its properties.

('object' and 'instance' in this context means the same)

By the way, you cannot in python actually destroy objects. You can delete variables with del a for example, but the objects are destroyed automatically when they are not referenced anymore.

  • Related