chenmeizhen 发表于 2022-5-24 15:01:58

'NoneType' object is not callable

import time
def time_master(func):
    print("开始运行程序。。。")
    start=time.time()
    func()
    stop=time.time()
    print("结束程序运行。。。")
    print(f"一共耗费{(stop-start):.2f}秒。")

@time_master
def myfunc():
    time.sleep(2)
    print("hello")

myfunc()

我按照视频写的代码,能运行出结果但还是报错了
TypeError: 'NoneType' object is not callable
请问怎么解决

specail 发表于 2022-5-24 16:24:53



@time_master语法糖的效果相当于#myfunc = time_master(myfunc)
所以你的代码执行时,代码运行了time_master函数,但是运行到最后一行的时候,myfunc 被复写成了 time_master(myfunc),所以 time_master(myfunc)()就报错了。

闭包部分代码应该改写成这个
import time
def time_master(func):
    def wraper(*args,**kwargs):
      print("开始运行程序。。。")
      start=time.time()
      func(*args,**kwargs)
      stop=time.time()
      print("结束程序运行。。。")
      print(f"一共耗费{(stop-start):.2f}秒。")
    return wraper
   
@time_master
def myfunc():
    time.sleep(2)
    print("hello")

myfunc()

chenmeizhen 发表于 2022-5-24 17:30:38

specail 发表于 2022-5-24 16:24
@time_master语法糖的效果相当于#myfunc = time_master(myfunc)
所以你的代码执行时,代码运行了t ...

{:10_298:}我知道错哪了!谢谢大佬!
页: [1]
查看完整版本: 'NoneType' object is not callable