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

 


posted @ 2016-03-17 17:55  Tom-Li  阅读(189)  评论(0编辑  收藏  举报