Python 包装函数会隐藏其属性吗?
这是我上一个问题的继续 如果我包装了一个函数,以便它记录使用以下装饰器调用它的次数:Python 包装函数会隐藏其属性吗?,python,python-3.x,decorator,wrapper,python-decorators,Python,Python 3.x,Decorator,Wrapper,Python Decorators,这是我上一个问题的继续 如果我包装了一个函数,以便它记录使用以下装饰器调用它的次数: 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):
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.wrapps()
只跨属性复制。然后,当递增包装函数上的计数器时,将为属性指定一个新的整数值,包装器仍将引用旧值
不要让包装
跨属性复制,而是让它跨函数的整个\uuuu dict\uuu
属性复制:
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
现在,包装函数fn
和fn\u包装器
对象共享可变的\uuuu dict\uuu
命名空间字典,对该字典所做的任何更改都可以在这两个函数中看到
assigned
是要复制的属性序列(它通常复制docstring、函数名和模块名等内容),而updated
是应该被视为字典的属性序列,这些字典是从包装函数更新的。后者通常设置为\uuu dict\uuuu
,但现在我们复制整个对象,不再需要从原始对象更新它。@MartijnPieters Updated。。。是的,我知道这一点,我基本上是在问如何分享它们:PAhh看起来像是我所希望的解决方案:)更新的updated=()
有什么作用?@MarkusMeskanen:更新了答案来解释这两个参数的含义。