我们在定义装饰器时都会使用 @wraps 让我们被装饰的函数保持自己的一些私有属性
def decorator(f):
@wraps(f)
def decorated(*args, **kwargs):
print('ok')
return f(*args, **kwargs)
return decorated
那么@wraps 是如何实现的呢,先看一下wraps 的代码:
WRAPPER_ASSIGNMENTS = ('__module__', '__name__', '__qualname__', '__doc__',
'__annotations__')
WRAPPER_UPDATES = ('__dict__',)
def wraps(wrapped,
assigned = WRAPPER_ASSIGNMENTS,
updated = WRAPPER_UPDATES):
"""Decorator factory to apply update_wrapper() to a wrapper function
Returns a decorator that invokes update_wrapper() with the decorated
function as the wrapper argument and the arguments to wraps() as the
remaining arguments. Default arguments are as for update_wrapper().
This is a convenience function to simplify applying partial() to
update_wrapper().
"""
return partial(update_wrapper, wrapped=wrapped,
assigned=assigned, updated=updated)
这里 @wraps 返回的也是partial(update_wrapper, wrapped=wrapped assigned=assigned, updated=updated),这里partial 是如何实现的,本人能力有限不作讨论,只要知道partial(update_wrapper, wrapped=wrapped assigned=assigned, updated=updated)(function)与 update_wrapper(function,wrapped=wrapped assigned=assigned, updated=updated)是一样的就可以了,接着看update_wrapper
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
我们按照最开始写的装饰器的代码把真实参数传进去
update_wrapper(wrapper, f, assigned = ('__module__', '__name__', '__qualname__', '__doc__', '__annotations__'), updated = ('__dict__',))
有了真实的参数后,我们再将它带入最开始的代码
def decorator(f):
@update_wrapper(wrapper, f, assigned = ('__module__', '__name__', '__qualname__', '__doc__', '__annotations__'), updated = ('__dict__',))
def decorated(*args, **kwargs):
print('ok')
return f(*args, **kwargs)
return decorated
这里的wrapper 就是我们要装饰的下面的函数decorated ,由于partial 的作用,上面的代码可以转换成如下:
def decorator(f):
update_wrapper(decorated, f, assigned = ('__module__', '__name__', '__qualname__', '__doc__', '__annotations__'), updated = ('__dict__',))
参照上面update_wrapper的实现我们可以知道,就是将f 的属性绑定到decorated 上去,因为我们使用装饰器时 :
@decorator
def login():
return 'hello'
相当于
login=decorator(login)=decorated
可以看到被装饰器修饰后的函数login已经变成了函数decorated,参考上面,可以知道,由于@wraps的存在,已经把f 即(函数login)的属性绑定到decorated 上了,此时调用被装饰后的函数login的属性得到的和装饰前的属性是一致的。
若在定义装饰器时没有使用@wraps
#from functools import wraps
def decorator(f):
# @wraps(f)
def decorated(*args, **kwargs):
print('ok')
return f(*args, **kwargs)
return decorated
@decorator
def test():
return 'ok'
print(test.__name__)
得到的值是 decorated 而使用了@wraps的值是test
本人菜鸟一个,如果有错欢迎指出。