高阶函数 + 嵌套函数 =》装饰器
import time
def timer(func):
def wrapper(*args, **kwargs):
start_time = time.time()
func(*args, **kwargs)
end_time = time.time()
print('consume time%s'%(end_time-start_time))
return func
return wrapper
@timer #deco = timer(deco) = wrapper 因此deco() = wrapper()
def deco(*args, **kwargs):
print('in the deco')
time.sleep(3)
return 'from shcool'
deco()