使用update_wrapper改變裝飾后函數(shù)的屬性
Python還提供定制化更強的函數(shù)update_wrapper
來改變裝飾后的函數(shù)屬性叙赚,使用示例驴一。
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函數(shù)源碼
參看源碼,正如之前的理解。
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
的實現(xiàn)機制。