I want to replace the class instance "model" with a new instance. This should happen in a reset function inside the class. I could easily do model = Model(number = 2) outside of the class, but that is not what I need.
The number thing is just an easy example for you. My actual task is totally different. In summary I need to create a new instance with the same name, because I need a new run of the __init__. And this has to happen inside the reset function. I hope you understand what I mean. My reset function does not work unfortunately:
class Model():
def __init__(self, number):
self.number = number
def reset(self):
self = Model(number = 2)
model = Model(number = 1)
print(model.number) #it is 1
model.reset()
print(model.number) #it is 1 aswell, should be 2 :(
self.number=2. But why isn'tmode = Model(2)what you need, precisely?self. You cannot replace object with another object in-place. Youresetshould really just set the instance to its desired / initial state by setting its attributes.