装饰器decorator
装饰器本质是函数,作用是为了其他函数添加附加功能
原则:不能改变其他函数的源代码和调用方式
包含知识内容:函数即变量,高阶函数,嵌套函数
1 import time 2 3 4 def timer(func): 5 def wraper(*args, **kwarge): # 函数的嵌套:函数里面定义函数 6 start_time = time.time() 7 func(*args, **kwarge) 8 stop_time = time.time() 9 print('The func running time is %s' % (stop_time - start_time)) 10 return wraper 11 12 13 @timer 14 def test(): 15 print('Begin to sleep..') 16 time.sleep(2) 17 print('in the test') 18 return 0 19 20 21 test()