Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to do a conditional decorator in python

Is it possible to decorator a function conditionally. For example, I want to decorate the function foo() with a timer function (timeit) only doing_performance_analysis is True (see the psuedo-code below).

if doing_performance_analysis:   @timeit   def foo():     """     do something, timeit function will return the time it takes     """     time.sleep(2) else:   def foo():     time.sleep(2)   
like image 713
cfpete Avatar asked May 23 '12 17:05

cfpete


People also ask

How do I create a custom decorator in Python?

To create a decorator function in Python, I create an outer function that takes a function as an argument. There is also an inner function that wraps around the decorated function. To use a decorator ,you attach it to a function like you see in the code below.

What is decorator in Python with example?

A decorator is a design pattern in Python that allows a user to add new functionality to an existing object without modifying its structure. Decorators are usually called before the definition of a function you want to decorate.

How does a decorator work in Python?

A decorator in Python is a function that takes another function as its argument, and returns yet another function . Decorators can be extremely useful as they allow the extension of an existing function, without any modification to the original function source code.

What is the correct syntax for using a decorator?

Decorators use a special syntax in JavaScript, whereby they are prefixed with an @ symbol and placed immediately before the code being decorated.


2 Answers

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.

like image 152
Martijn Pieters Avatar answered Sep 16 '22 14:09

Martijn Pieters


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) 
like image 42
Blckknght Avatar answered Sep 20 '22 14:09

Blckknght