python decorate function call

Question:

Is it possible to wrap a function call using python decorators?

I don’t want to implement a wrapper for every function of a module individually.

I would like to have something like

def a(num):
    return num

@double_the_value
a(2)

returning 4 without the need of having access to the implementation of a.

Would a global wrapper like

def multiply(factor, function, *args, **kwargs):
    return factor * function(*args, **kwargs)

be the better choice in this case?

Asked By: Dschoni

||

Answers:

You could do something like that:

def a(num):
    return num * 1

def double(f):
    def wrapped(*args, **kwargs):
        return f(*args, **kwargs)
    return wrapped

print(double(a)(2))

It’s because we can decorate functions and run functions using a decorator function explicit as in the example above.
So in this one:

print(double(a)(2))

In the place of a you can put any function and in place of the 2, args and kwargs.

Answered By: turkus

While the @decorator syntax is only available to be used in conjunction with the definition of a function or class, the syntax before decorators became a language feature can do what you request:

from module import myfunc

myfunc = double_decorator(myfunc)

x = myfunc(2) # returns 4

Further Reading: There is a very good detailed section on decorators in Marty Alchin’s book Pro Python from Apress.

Answered By: joel goldstick

I find that a useful approach is to define a new function that is decorated

def my_function():
    pass

@my_decorator
def my_function_with_decorator(*args, **kwargs):
    my_function()

my_function() # call without decorator

my_function_with_decorator() # call with decorator
Answered By: Harry Berg
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.