Day4-装饰器高阶函数嵌套函数

# import time
#
# def bar():
# time.sleep(3)
# print("in the bar")
#
# def test1(func):
# start_time = time.time()
# func()
# stop_time = time.time()
# print("the func run time is %s"%(stop_time-start_time))
#
# test1(bar)

import time

def timer(func): #timer(test1) func = test1
def deco():
start_time = time.time()
func()
stop_time = time.time()
print("the func run time is %s"%(stop_time-start_time))
return deco # return deco 是返回deco的物理地址, test1, test1() 的区别是test1表示函数的物理地址, test1() 调用函数
@timer #test1=timer(test1)
def test1():
time.sleep(3)
print("in the test1")
@timer
def test2():
time.sleep(3)
print("in the test2")
#test1() # -> deco
test1()
test2()

猜你喜欢

转载自www.cnblogs.com/carol7258/p/12972808.html