Say I have a class like
class Thing():
def __init__(self):
self.some_state = False
def do_stuff(self):
self.some_state = True
# do stuff which may take some time - and user may quit here
self.some_state = False
def do_other_stuff(self):
# do stuff which depends on `some_state` being False
And I want to make sure that if a user executes this in a notebook by running:
thing = Thing()
thing.do_stuff()
then presses "stop execution" while running, some_state
toggles back to False
. That way do_other_stuff
will work as intended. Is there a way to do some graceful clean up?
Note: Although my example is quite specific, my question is generally: "Can I do graceful cleanup?"