I am looking for a pointer on an appropriate way of resetting an object (planning code currently). What I currently have thought of is below. The only problem is that there are many other attributes that are defined in other methods that don't get deleted when I recall init. That isn't a problem for the way I structured my object (all the attributes not defined in init are always recalculated when simulation method is run). But, I don't feel like it's clean - I would prefer to do a full reset to the initialization state and not have any attributes defined outside of init.
class foo:
def __init__(self, formaat):
self.format == formaat
# process format below:
if formaat == one:
self.one = 1
if formaat == two:
self.two = 2
# ... other parameter imports below - dependent on the value of self.one/self.two
def reset(self, formaat):
self.__init__(formaat)
def simulate(self):
self.reset(self.format)
print("doing stuff")
One thing I tried is having a method to copy itself. Although I think there is literally no difference between doing this and making a copy of the object in the run script and reassigning it.
class foo:
def __init__(self, formaat):
self.format = formaat
# process format below:
if formaat == one:
self.one = 1
if formaat == two:
self.two = 2
# ... other parameter imports below
def copymyself(self):
self.copy = copy.deepcopy(foo(self.format))
def simulate(self):
print("doing stuff")
Ideally I want to have the simulate method reset itself at the start every time. In the example code above I would have to do the following run script.
a = foo()
# loop the code below
a.copymyself()
a.simulate()
a = a.copy
I much rather prefer to have a single line - a.simulate() like in the case with the reset method.