In the following code the methods(print_a and print_b) of class Test, has been decorated by the two different decorators.
How can I determine that the given method(let say print_a), is decorated with some specific decorator (decorator1) at the runtime?
The easy solution is to change the name of the wrapper function, i.e. changing the wrapper to wrapper1 and wrapper2 in the decorator method, but the problem with that is I don't have control over that part of the code.
Does python have any reflection API like Java, and could that help me here ?
def my_decorator1(func): def wrapper(*args, **kwargs): print('I am decorated:1') func(*args, **kwargs) return wrapper def my_decorator2(func): def wrapper(*args, **kwargs): print('I am decorated:2') func(*args, **kwargs) return wrapper class Test(): def __init__(self, a=None, b=None): self.a = a self.b = b @my_decorator1 def print_a(self): print('Value of a is {}'.format(self.a)) @my_decorator2 def print_b(self): print('Value of b is {}'.format(self.b)) if __name__ == '__main__': d = Test.__dict__ f1 = d.get('print_a') f2 = d.get('print_b')