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

decorate 运行跳出错误,到底错在哪里?

  1. import time
    def performance(f):
        def fn(*args,**kw):
            t1=time.time()
            r=f(*args,**kw)
            t2=time.time()
            print "call %s() in %fs" %(f._name_,(t2-t1))
            return r
        return fn

  2. @performance
    def factorial(n):
        return reduce(lambda x,y:x*y,range(1,n+1))
    print factorial(10)

  3. Traceback (most recent call last):
      File "E:/learn python/practice4.py", line 14, in <module>
        print factorial(10)
      File "E:/learn python/practice4.py", line 7, in fn
        print "call %s() in %fs" %(f._name_,(t2-t1))
    AttributeError: 'function' object has no attribute '_name_'



正在回答

1 回答

f._name_ 写错了吧,应该是 f.__name__, 两个下划线

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

举报

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

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

进入课程

decorate 运行跳出错误,到底错在哪里?

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

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

帮助反馈 APP下载

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

公众号

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