总而言之,用白话来说,@wraps相当于装饰器的装饰器。
python内置方法的使用解释看起来很复杂() ) ) ) ) )。
efwraps(wrapped,
assigned=WRAPPER_ASSIGNMENTS,
updated=wrapper_updates(:
' ' ' decoratorfactorytoapplyupdate _ wrapper () to a wrapper function
returnsadecoratorthatinvokesupdate _ wrapper () with the decorated
functionasthewrapperargumentandtheargumentstowraps () as the
remaining arguments.defaultargumentsareasforupdate _ wrapper ()。
thisisaconveniencefunctiontosimplifyapplyingpartial () to
update_wrapper (。
“”'
returnpartial(update_wrapper,wrapped=wrapped,
assigned=assigned,updated=updated )
举个例子:
from functools import wraps
#验证用户的角色权限
def permission_required () :
defdecorator(f ) :
#@wraps(f )
def decorated_function () :
返回‘thisisdecoratedfunction’
return decorated_function
返回解码器
#管理员权限确认
efadmin_required(f ) :
return permission_required (
if __name__==‘__main__‘:
eftest_func(:
返回‘this is test function’
打印(admin _ required (test _ func ) )
如果不使用@wraps,则打印的是源xxdkh
. decorator . decorated _ functionat0x 0000000030411 e 0
用了之后感觉很清爽
通过比较结果,大致可以推测@wraps的作用。
然后返回参数test_func方法的地址。 这意味着,如果要使用参数方法的某些属性,则必须使用@wraps。