Say I wanted to alter an instance variable after running each method in a class. What is the best way to do this? I am aware of how to use decorators (at a very basic level), but don't know how to modify instance variables from a class using them.
As an example, I want to achieve what is shown below, but without explicitly calling post_method_routine from each method
class MyClass():
def __init__(self):
self.state = True
def post_method_routine(self):
self.state = False
print(self.state)
def method1(self):
# do stuff
self.post_method_routine()
def method2(self):
# do stuff
self.post_method_routine()
def methodN(self):
# do stuff
self.post_method_routine()
myinst = MyClass()
myinst.method1() # Output: False