为了账号安全,请及时绑定邮箱和手机立即绑定

完善decorator

import time, functools


def performance(unit):

    def performance_decorator(f):

        @functools.wraps(f)

        def wrapper(*agrs,**kw):

            t1=time.time()

            r=f(*agrs,**kw)

            t2=time.time()

            t=(t2-t1)*1000 if unit=='ms' else (t2-t1)

            print 'call %s() %s %s' % (f.__name__,t,unit)

            return r

        return wrapper

    return performance_decorator


@performance('ms')

def factorial(n):

    return reduce(lambda x,y: x*y, range(1, n+1))

print factorial(2)

print factorial.__name__

为什么在第二行会输出 2 这个结果呢


正在回答

1 回答

因为先要打印wrapper里面的print, 然后返回值出来,打印代码倒数第二行的print

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消
python进阶
  • 参与学习       255533    人
  • 解答问题       3038    个

学习函数式、模块和面向对象编程,掌握Python高级程序设计

进入课程

完善decorator

我要回答 关注问题
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号