新手关于拓展阅读修饰符的疑问
新手求教各位大佬:1.为什么还要在 timeslong 函数里再定义一个 call 函数?
import time
def timeslong(func):
def call():
start = time.clock()
print("It's time starting ! ")
func()
print("It's time ending ! ")
end = time.clock()
return "It's used : %s ." % (end - start)
return call
@timeslong
def f():
y = 0
for i in range(10):
y = y + i + 1
print(y)
return y
print(f())
2.如果代码修改如下,为什么不执行“return "It's used : %s ." % (end - start)”?
import time
def timeslong(func):
start = time.perf_counter()
print("It's time starting ! ")
func()
print("It's time ending ! ")
end = time.perf_counter()
return "It's used : %s ." % (end - start)
def f():
y = 0
for i in range(10):
y = y + i + 1
print(y)
return y
timeslong(f)
将第 20 行的
timeslong(f)
改为
print(timeslong(f))
就可以了吧
页:
[1]