我试图在没有运气的情况下访问包装函数内的装饰器参数。
我所拥有的是:
def my_decorator(arg1=False, arg2=None):
def decorator(method):
@functools.wraps(method)
def wrapper(method, *args, **kwargs):
# do something based on arg1 and arg2
# accessing one of the two named arguments
# ends up in a 'referenced before assignment'
arg1 = arg1 # error
arg2 = arg2 # error
newarg1 = arg1 # working
newarg2 = arg2 # working
return method(*args, **kwargs)
return wrapper
return decorator
我会像普通装饰器一样使用它
@my_decorator(arg1=True, arg2='a sting or whatever else')
the_function()
我真的不明白为什么我不能访问装饰器参数。