1. 程式人生 > >30 裝飾器終極版本(進階)

30 裝飾器終極版本(進階)

import time
FLAGE = False
def timmer_out(flag):
def timmer(func):
def inner(*args,**kwargs):
if flag:
start = time.time()
ret = func(*args,**kwargs)
end = time.time()
print(end-start)
return ret
else:

ret = func(*args, **kwargs)
return ret
return inner
return timmer
@timmer_out(FLAGE)
def wahaha():
time.sleep(0.1)
print("wahhhhahahahahh")
@timmer_out(FLAGE)
def erguotou():
time.sleep(0.1)
print("erguotoutoutotuou")
wahaha()
erguotou()