Python通用装饰器

 1 #author F
 2 import time
 3 
 4 
 5 def timer(func):
 6     def deco(*args, **kwargs): #保证传递所有参数都能使用
 7         start_time = time.time()
 8         func(*args, **kwargs)
 9         stop_time = time.time()
10         print("cost time is %s" %(stop_time - start_time))
11     return deco
12 
13 @timer  # test=timer(test)
14 def test(name, sex):
15     print("{}'s sex is {}".format(name, sex))
16 
17 test(1, 2)

 

posted @ 2017-06-15 17:26  Bird_getUpEarly  阅读(134)  评论(0编辑  收藏  举报