裝飾器就是給原理的函數添加新的功能,用到了閉包
#需要添加的功能
def show_time(fun):
def inner():
start = time.time()
fun()
end = time.time()
print(end-start)
return inner
#功能函數
def foo():
print('foo ...')
time.sleep(2)
#不改變原來的調用方式
foo = show_time(foo)
#調用函數
foo()