python中wraps_【Python】Flask中@wraps的使用

先说总结,白话来讲,@wraps相当于是装饰器的装饰器。

python内置的方法使用解释,看起很复杂的样子┓( ´∀` )┏

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)

下面举个栗子:

from functools import wraps

# 用户角色权限确认

def permission_required():

def decorator(f):

#@wraps(f)

def decorated_function():

return 'this is decorated function'

return decorated_function

return decorator

# 管理员权限确认

def admin_required(f):

return permission_required()(f)

if __name__=='__main__':

def test_func():

return 'this is test function'

print(admin_required(test_func))

不用@wraps时,打印出来是酱婶儿的

.decorator..decorated_function at 0x00000000030411E0>

使用之后看起来清爽了很多

通过对比结果,@wraps的作用猜出来个大概。

使用之后它返回的是参数test_func方法的地址。也就是说,如果你想使用参数方法的一些属性,就需要使用@wraps了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值