访问装饰器包装函数内的装饰器参数
我试图访问包装函数内的装饰器参数,但没有成功。
我所拥有的是:
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()
我真的不明白为什么我无法访问装饰器参数。
im trying to access my decorators arguments inside the wrapper function with no luck.
what i have is:
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
and i would use it like a normal decorator
@my_decorator(arg1=True, arg2='a sting or whatever else')
the_function()
i really don't understand why i can't access the decorators arguments.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以访问 arg1 和 arg2,但您不应该分配给这些名称,即使使用“增强”赋值运算符也不行,因为这会使它们成为内部函数中的局部变量。您收到的错误消息表明您尝试执行此操作(尽管您没有显示代码)。
在 Python 3 中,您可以使用
wrapper()
中的某个位置来解决此问题。
You can access
arg1
andarg2
, but you should not assign to these names, not even with "augmented" assign operators, because this would make them local variables in the inner function. The error message you get shows that you tried to do exactly this (though you did not show your code).In Python 3, you can work around this problem using
somewhere in
wrapper()
.这取决于您对arg1
和arg2
的处理方式。一般来说,闭包可以完美地工作,不需要额外的工作。但是,如果您在内部函数中重新分配它们,Python 会假定它是局部变量,您必须否则告诉它。在 Python 3 中,声明非局部 arg1, arg2
。在Python 2中,你必须作弊:将两者包装到列表中(外部函数中的arg1 = [arg1]
)并在内部函数中使用arg1[0]
。如果您想了解其工作原理的解释,请搜索有关此主题的 Python 问题或参考文档(我相信我会在语言参考中的某个位置进行搜索)。您的问题是
wrapper
将self
传递给method
。没有自我
。你必须接受它(即使如此,你也会将装饰器限制为方法 - 为什么不让self
滑入*args
呢?)。我看不出您如何从“全局名称
self
未定义”中读取“赋值前引用”...It depends on what you do witharg1
andarg2
. Generally, closures work perfectly without extra work. However, if you re-assign them in the inner function, Python assumes it's a local variable and you have to tell it otherwise.In Python 3, declarenonlocal arg1, arg2
. In Python 2, you have to cheat: Wrap the two into lists (arg1 = [arg1]
in the outer function) and usearg1[0]
in the inner function. If you want an explanation of why this works, either search for Python questions on this topic or refer to the documentation (somewhere in the language reference I believe, I'll search).Your problem is that
wrapper
passesself
tomethod
. There's noself
. You'd have to take it (and even then, you'd restrict the decorator to methods - why not letself
slip into*args
?).I fail to see how you read "referenced before assignment" from "global name
self
is not defined"...