复习decorator

装饰器decorator复习,传递参数:

 1 import time
 2 
 3 def timer(argv):
 4     def out_wraper(func):
 5         def wraper(a, b):
 6             print('In the %s' % func)
 7             start_time = time.time()
 8             time.sleep(2)
 9             func(a, b)
10             stop_time = time.time()
11             print('the running time  of %s is:%s' %(argv,(stop_time - start_time)))
12         return wraper
13     return out_wraper
14 
15 @timer('add')
16 def add(a, b):
17     print(a + b)
18 
19 
20 @timer('sub')
21 def sub(a, b):
22     print(a - b)
23 
24 
25 add(3, 5)
26 sub(8, 3)
View Code
原文地址:https://www.cnblogs.com/gzj137070928/p/13710391.html