how to do a conditional decorator in python
Decorators are simply callables that return a replacement, optionally the same function, a wrapper, or something completely different. As such, you could create a conditional decorator:
def conditional_decorator(dec, condition): def decorator(func): if not condition: # Return the function unchanged, not decorated. return func return dec(func) return decorator
Now you can use it like this:
@conditional_decorator(timeit, doing_performance_analysis)def foo(): time.sleep(2)
The decorator could also be a class:
class conditional_decorator(object): def __init__(self, dec, condition): self.decorator = dec self.condition = condition def __call__(self, func): if not self.condition: # Return the function unchanged, not decorated. return func return self.decorator(func)
Here the __call__
method plays the same role as the returned decorator()
nested function in the first example, and the closed-over dec
and condition
parameters here are stored as arguments on the instance until the decorator is applied.
A decorator is simply a function applied to another function. You can apply it manually:
def foo(): # whatever time.sleep(2)if doing_performance_analysis: foo = timeit(foo)
How about:
def foo(): ...if doing_performance_analysis: foo = timeit(foo)
I imagine you could even wrap this into a decorator that would take a boolean flag and another decorator, and would only apply the latter if the flag is set to True
:
def cond_decorator(flag, dec): def decorate(fn): return dec(fn) if flag else fn return decorate@cond_decorator(doing_performance_analysis, timeit)def foo(): ...