0

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.

1
  • 1
    Use a class to define a template for the "clean" initial object. Everytime you need to clean the object, just make a new instance of the class, and use the new object instead. That is how you should be using your classes and objects. Unless there is a very special requirement (like not instantiating more than one very heavy object; but as you are cleaning it, this does not look to apply here). Commented Apr 7, 2020 at 14:52

1 Answer 1

1

Every new run deserves a new (clean) object.

Sign up to request clarification or add additional context in comments.

1 Comment

While very short, this is actually the right answer, and seen from another angle, it is a very concise one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.