Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how should I user python decorator without changing the function name?

def decorate(func):
    def wrapper(*args, **kwargs):
        return func(*args, **kwargs)
    return wrapper

@decorate
def test(a=1,b=2):
    return a+b

print test.__name__

the result is wrapper. Is there any way make the result is "test"?

like image 807
马新民 Avatar asked Dec 01 '25 02:12

马新民


1 Answers

Use functools.wraps:

from functools import wraps

def decorate(func):
    @wraps(func)
    def wrapper(*args, **kwargs):
        return func(*args, **kwargs)
    return wrapper

@decorate
def test(a=1,b=2):
    return a+b

print test.__name__
like image 86
Platinum Azure Avatar answered Dec 02 '25 14:12

Platinum Azure



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!