Python类自定义函数修饰器(转)

 1 class Tx(object):
 2     def __init__(self):
 3         print("Tx:init")
 4         self.something()
 5 
 6     def something(self):
 7         print("Tx:something")
 8 
 9     def otherthing(self):
10         print("Tx:otherthing")
11 
12     def __call__(self, func):
13         def wrapper(s, url_list):
14             url_list.append(4)
15             self.otherthing()
16             return func(s, url_list)
17         return wrapper
18 
19 
20 class v2(object):
21     @Tx()
22     def run(self, url_list):
23         print("v2:run")
24         print(url_list)
25 
26 
27 x = v2()
28 x.run([1, 2, 3])
29 
30 #####
31 # OUTPUT
32 #
33 # Tx:init
34 # Tx:something
35 # Tx:otherthing
36 # v2:run
37 # [1, 2, 3, 4]
38 #
39 #####

转:https://gist.github.com/phith0n/afe56234e3301435c3dd
posted @ 2020-05-06 23:58  Loa5  阅读(14)  评论(0)    收藏  举报