我应该如何在不更改函数名称的情况下使用 python 装饰器?

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__

结果是包装器。有什么办法让结果是"test"?

使用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__