不改变原来的代码,给函数添加新功能
demo11
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19import time
def timer(func):
def war(*args, **kwargs):
start_time = time.time()
res = func(*args,**kwargs)
end_time = time.time()
timeNumber = float(end_time - start_time)
print("%s函数运行的时间是%3f"%(func.__name__,timeNumber))
return res
return war
@timer
def sleep(number):
number = int(number)
time.sleep(number)
return "我睡了%s秒"%number
sleep(2)