使用update_wrapper改变装饰后函数的属性
Python还提供定制化更强的函数update_wrapper
来改变装饰后的函数属性,使用示例。
from functools import update_wrapper
def add_dec(f):
def wrapper(*args, **kwargs):
'wrapper'
print('new wrapper')
return f(*args, **kwargs)
return update_wrapper(wrapper, f)
@add_dec
def add(x, y):
'original add'
return x + y
print(add(3, 3))
print(add)
print(add.__name__)
print(add.__doc__)
new wrapper
6
<function add at 0x7f00931a20d0>
add
original add
看样子效果跟使用@wraps(f)
是一样的
update_wrapper函数源码
参看源码,正如之前的理解。
WRAPPER_ASSIGNMENTS = ('__module__', '__name__', '__qualname__', '__doc__',
'__annotations__')
WRAPPER_UPDATES = ('__dict__',)
def update_wrapper(wrapper,
wrapped,
assigned = WRAPPER_ASSIGNMENTS,
updated = WRAPPER_UPDATES):
"""Update a wrapper function to look like the wrapped function
wrapper is the function to be updated
wrapped is the original function
assigned is a tuple naming the attributes assigned directly
from the wrapped function to the wrapper function (defaults to
functools.WRAPPER_ASSIGNMENTS)
updated is a tuple naming the attributes of the wrapper that
are updated with the corresponding attribute from the wrapped
function (defaults to functools.WRAPPER_UPDATES)
"""
for attr in assigned:
try:
value = getattr(wrapped, attr)
except AttributeError:
pass
else:
setattr(wrapper, attr, value)
for attr in updated:
getattr(wrapper, attr).update(getattr(wrapped, attr, {}))
# Issue #17482: set __wrapped__ last so we don't inadvertently copy it
# from the wrapped function when updating __dict__
wrapper.__wrapped__ = wrapped
# Return the wrapper so this can be used as a decorator via partial()
return wrapper
默认拷贝了'__module__', '__name__', '__qualname__', '__doc__','__annotations__'
属性。
结论
熟悉update_wrapper
有助于理解@wraps
的实现机制。