Day4-装饰器

'''
装饰器:
本质是个函数, 为其他函数添加附加功能
原则:
1.不能修改被装饰的函数的源代码
2.不能修改被装饰的函数的调用方式

实现装饰器知识储备:
1.函数即变量
2.高阶函数
3.嵌套函数
高级函数+嵌套函数 =》 装饰器
'''
import time

def timer(func):
def warpper(*args, **kwargs):
start_time = time.time()
func()
stop_time = time.time()
print("the func run time is %s"%(stop_time-start_time))
return warpper()
@timer
def test1():
time.sleep(3)
print("in the test1")

test1()
posted @ 2020-05-27 14:19  Carol7258  阅读(86)  评论(0)    收藏  举报