我已经做了两个功能做同样的事情,但不同。我想比较每个函数运行的时间,所以我添加了一个装饰器@calcul_time。该脚本确实工作,但我得到以下错误信息:Python装饰器离开NoneType错误
{1: 1, 2: 3, 3: 2, 4: 4, 5: 4, 6: 3, 7: 3, 8: 2, 9: 2, 10: 2, 11: 2, 12: 2, 13: 2, 14: 2, 15: 2, 16: 2, 17: 2, 18: 2, 19: 2, 20: 2, 21: 2, 22: 2, 23: 0, 24: 0}
0.0021219253540039062
{1: 1, 2: 3, 3: 2, 4: 4, 5: 4, 6: 3, 7: 3, 8: 2, 9: 2, 10: 2, 11: 2, 12: 2, 13: 2, 14: 2, 15: 2, 16: 2, 17: 2, 18: 2, 19: 2, 20: 2, 21: 2, 22: 2, 23: 0, 24: 0}
8.702278137207031e-05
Traceback (most recent call last):
File "./03_PeakHours.py", line 51, in <module>
horaires1()
TypeError: 'NoneType' object is not callable
我不明白为什么我有这种NoneType错误消息。如果我评论我的装饰器,我没有任何错误。以下是我的脚本。有谁知道我为什么会得到这个'NoneType'错误?
#!/usr/local/bin/python3.5
import time
input='''5
1 8
2 3
4 23
4 6
2 23'''
def calcul_time(fonction):
avant = time.time()
fonction()
apres = time.time()
print(apres - avant)
#@calcul_time
def horaires1():
hours = {}
for time in range(1,25):
hours[time] = 0
def inBetween(line):
current = int(line.split(" ")[0])
while current < int(line.split(" ")[1]):
hours[current] +=1
current += 1
list(map(inBetween, input.split("\n")[1:]))
print(hours)
return 0
#@calcul_time
def horaires2():
lines = input.split("\n")
hours={}
for time in range(1,25):
hours[time] = 0
for i in range(1, int(lines[0])+1):
start, stop = lines[i].split(" ")
for heure in range(int(start), int(stop)):
hours[heure] += 1
print(hours)
return 0
horaires1()
horaires2()
Euh这不是一个装饰者如何工作... –
'calcul_time'永远不会返回任何东西,因此'NoneType'错误,因为没有返回的方法总是返回'None'。装饰者应该返回一个可调用的 –