访问装饰器包装函数内的装饰器参数

发布于 2024-10-17 09:17:22 字数 762 浏览 3 评论 0原文

我试图访问包装函数内的装饰器参数,但没有成功。

我所拥有的是:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

木槿暧夏七纪年 2024-10-24 09:17:22

您可以访问 arg1 和 arg2,但您不应该分配给这些名称,即使使用“增强”赋值运算符也不行,因为这会使它们成为内部函数中的局部变量。您收到的错误消息表明您尝试执行此操作(尽管您没有显示代码)。

在 Python 3 中,您可以使用 wrapper() 中的某个位置来解决此问题

nonlocal arg1, arg2

You can access arg1 and arg2, 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

nonlocal arg1, arg2

somewhere in wrapper().

ぺ禁宫浮华殁 2024-10-24 09:17:22

这取决于您对 arg1arg2 的处理方式。一般来说,闭包可以完美地工作,不需要额外的工作。但是,如果您在内部函数中重新分配它们,Python 会假定它是局部变量,您必须否则告诉它。

在 Python 3 中,声明非局部 arg1, arg2 。在Python 2中,你必须作弊:将两者包装到列表中(外部函数中的arg1 = [arg1])并在内部函数中使用arg1[0]。如果您想了解其工作原理的解释,请搜索有关此主题的 Python 问题或参考文档(我相信我会在语言参考中的某个位置进行搜索)。

您的问题是 wrapperself 传递给 method。没有自我。你必须接受它(即使如此,你也会将装饰器限制为方法 - 为什么不让 self 滑入 *args 呢?)。

我看不出您如何从“全局名称 self 未定义”中读取“赋值前引用”...

It depends on what you do with arg1 and arg2. 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, declare nonlocal arg1, arg2. In Python 2, you have to cheat: Wrap the two into lists (arg1 = [arg1] in the outer function) and use arg1[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 passes self to method. There's no self. You'd have to take it (and even then, you'd restrict the decorator to methods - why not let self slip into *args?).

I fail to see how you read "referenced before assignment" from "global name self is not defined"...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文