gpt4 book ai didi

python - 包装一个函数隐藏它的属性?

转载 作者:太空宇宙 更新时间:2023-11-03 15:10:08 25 4
gpt4 key购买 nike

这是我之前问题的延续 Change an attribute of a function inside its own body .

如果我包装了一个函数,以便它使用以下装饰器记录它被调用的次数:

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

有什么解决办法吗?比如“不断地”重新包装,或者更好的东西?

最佳答案

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 是要复制的属性序列(它通常复制文档字符串、函数名称和模块名称等内容),updated 是应该像字典一样对待的属性,其中这些字典是从包装函数更新的。后者通常设置为 __dict__,但现在我们复制了整个对象,我们不再需要从原始对象更新它。

关于python - 包装一个函数隐藏它的属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27940460/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com