python decorate function call
Asked Answered
M

3

20

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?

Mcmichael answered 7/9, 2016 at 13:0 Comment(1)
I'd use your multiply(factor, function), since what you're asking is not possible.Fleabitten
R
19

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.

Rosewood answered 7/9, 2016 at 13:48 Comment(0)
S
10

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.

Soft answered 7/9, 2016 at 13:9 Comment(3)
How would I use that now together with the @ syntax?Mcmichael
In your case you can't. You can use @ syntax only on function declaration.Soft
@turkus: perhaps you should change it so that the wrapper actually returns double the value? return 2 * f(*args, **kwargs). Or call the decorator something else, like do_nothing.Mcglone
V
-4

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
Verona answered 14/5, 2022 at 11:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.