包装函数会隐藏其属性吗?

2024-04-24 02:38:40 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我上一个问题Change an attribute of a function inside its own body的继续。你知道吗

如果我包装了一个函数,这样它就可以使用下面的decorator记录调用它的次数:

def keep_count(f):
    @wraps(f)
    def wrapped_f(*args, **kwargs):
        f(*args, **kwargs)
        wrapped_f.count += 1
    wrapped_f.count = 0
    return wrapped_f

然后我想用别的东西把它包起来:

def decorator2(fn):
    @wraps(fn)
    def fn_wrapper(*args, **kwargs):
        if my_condition(fn):
            fn(*args, **kwargs)
    return fn_wrapper

test_f = decorator2(test_f)

我不能再像我希望的那样访问函数的count属性了。 count属性的当前值通过@wraps(fn)复制,但是如果我再次调用该函数,则计数将在原始函数内递增,但新值不会复制到新的修饰函数。你知道吗

>>> test_f()
() {}
>>> test_f.count
1
>>> test_f = decorator2(test_f)
>>> test_f.count  # The 1 gets copied here
1
>>> test_f()  # Only the inner function's count increments...
() {}
>>> test_f.count  # Still one, tho it should be two
1

有什么解决办法吗? 比如“不断”重拍,或者更好的?你知道吗


Tags: 函数testreturn属性defcountargsfunction
1条回答
网友
1楼 · 发布于 2024-04-24 02:38:40

functools.wraps()属性复制。然后在包装函数上增加计数器时,将为属性指定一个新的整数值,包装器仍将引用旧值。你知道吗

与其跨属性进行wraps复制,不如跨函数的整个__dict__属性进行复制:

from functools import wraps, WRAPPER_ASSIGNMENTS

def decorator2(fn):
    @wraps(fn, assigned=WRAPPER_ASSIGNMENTS + ('__dict__',), updated=())
    def fn_wrapper(*args, **kwargs):
        if my_condition(fn):
            fn(*args, **kwargs)
    return fn_wrapper

现在,包装函数fnfn_wrapper对象共享可变的__dict__命名空间字典,对该字典所做的任何更改都可以在这两个函数中看到。你知道吗

assigned是要跨其复制的属性序列(它通常复制docstring、函数名和模块名等内容),updated是应该像字典一样对待的属性序列,这些字典是从包装函数更新的。后者通常设置为__dict__,但是现在我们跨整个对象进行复制,我们不再需要从原始对象更新它。你知道吗

相关问题 更多 >