python 的装饰器解释
装饰器:
1 import time 2 def timeit(fn): 3 def wrap(*args,**kwargs): 4 start = time.time() 5 fn(*args,**kwargs) 6 print (time.time() - start) 7 return wrap 8 9 def sleep(x): 10 time.sleep(x)
11 timeit(sleep)(3) 12 3.003286123275757 13 14 15 @timeit 16 def sleep(x): 17 time.sleep(x) 18 19 sleep(2) 20 21 22 2.00276517868042
分解开就是:
fn = timeit(sleep)
fn(3)
3.00378155708313