Do something at the beginning & end of methods

Viewed 2287

Is there an easy way to do something at the beginning and end of each function in a class? I've looked into __getattribute__, but I don't think that I can use it in this situation?

Here's a simplified version of what I'm trying to do:

class Thing():
    def __init__(self):
        self.busy = False

    def func_1(self):
        if self.busy: 
            return None
        self.busy = True
          ...
        self.busy = False

    def func_2(self):
        if self.busy: 
            return None
        self.busy = True
          ...
        self.busy = False
    ...
2 Answers
Related