通俗易懂的分析——python装饰器之@functools.warps

首先,宝宝觉着网上没有比我这还透彻的@functools.wraps分析了~~~害羞自恋ing……

栗子详情:http://stackoverflow.com/questions/308999/what-does-functools-wraps-do
step1:

def logged(func):
    def with_logging(*args, **kwargs):
        print func.__name__ + " was called"
        return func(*args, **kwargs)
    return with_logging
@logged
def f(x):
   """does some math"""
   return x + x * x

it's exactly the same as saying

def f(x):
    """does some math"""
    return x + x * x
f = logged(f)

这时(划重点!),your function f is replaced with the function with_logging.


print f.__name__ #with_logging
print f.__doc__  #什么也没有,因为with_logging没有doc,f才有doc

因为f方法的name doc args也变了
简言之:使用装饰器时,原函数会损失一些信息,

  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值