【发布时间】:2011-06-25 04:16:01
【问题描述】:
我试图在包装函数中访问我的装饰器参数,但没有成功。
我拥有的是:
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()
我真的不明白为什么我不能访问装饰器参数。
【问题讨论】:
标签: python arguments decorator optional-arguments