python装饰器

  1 #coding=utf-8
  2 import sys
  3 '''
  4 Created on 2016年8月7日
  5 
  6 @author: yongliu
  7 '''
  8 
  9 def deco(func):
 10     print("before myfunc() called.")
 11     func()
 12     print("after myfunc() called.")
 13     return func
 14 
 15 @deco
 16 def myfunc():
 17     print("myfunc() called." )
 18     return 'ok'
 19 
 20     
 21 def deco2(func):
 22     def _deco():
 23         print("before myfunc() called.")
 24         ret = func()
 25         print("after myfunc() called.")
 26         return ret
 27     return _deco
 28 
 29 @deco2
 30 def myfunc2():
 31     print("myfunc2() called.")
 32     return 'ok'
 33 
 34 
 35 
 36 def deco3(func):
 37     def _deco(a, b):
 38         print("before %s called." % func.__name__)
 39         ret = func(a, b)
 40         print("after %s called. result: %s" % (func.__name__, ret))
 41         return ret
 42     return _deco
 43  
 44 @deco3
 45 def myfunc3(a, b):
 46     print(" myfunc3(%s,%s) called." % (a, b))
 47     return a + b
 48 
 49 def deco4(func):
 50     def _deco(*args, **kwargs):
 51         print("before %s called." % func.__name__)
 52         ret = func(*args, **kwargs)
 53         print("after %s called. result: %s" % (func.__name__, ret))
 54         return ret
 55     return _deco
 56  
 57 @deco4
 58 def myfunc41(a, b):
 59     print(" myfunc41(%s,%s) called." % (a, b))
 60     return a+b
 61  
 62 @deco4
 63 def myfunc42(a, b, c):
 64     print(" myfunc42(%s,%s,%s) called." % (a, b, c))
 65     return a+b+c
 66  
 67 def deco5(arg):
 68     def _deco(func):
 69         def __deco():
 70             print("before %s called [%s]." % (func.__name__, arg))
 71             func()
 72             print("  after %s called [%s]." % (func.__name__, arg))
 73         return __deco
 74     return _deco
 75  
 76 @deco5("mymodule")
 77 def myfunc51():
 78     print(" myfunc51() called.")
 79  
 80 @deco5("module2")
 81 def myfunc52():
 82     print(" myfunc52() called.")
 83     
 84     
 85     
 86 class locker:
 87     def __init__(self):
 88         print("locker.__init__() should be not called.")
 89          
 90     @staticmethod
 91     def acquire():
 92         print("locker.acquire() called.(这是静态方法)")
 93          
 94     @staticmethod
 95     def release():
 96         print("locker.release() called.(不需要对象实例)")
 97  
 98 def deco6(cls):
 99     '''cls 必须实现acquire和release静态方法'''
100     def _deco(func):
101         def __deco():
102             print("before %s called [%s]." % (func.__name__, cls))
103             cls.acquire()
104             try:
105                 return func()
106             finally:
107                 cls.release()
108         return __deco
109     return _deco
110  
111 @deco6(locker)
112 def myfunc6():
113     print("myfunc6() called.")
114  
115  
116  
117  
118  
119 class mylocker7:
120     def __init__(self):
121         print("mylocker7.__init__() called.")
122          
123     @staticmethod
124     def acquire():
125         print("mylocker7.acquire() called.")
126          
127     @staticmethod
128     def unlock():
129         print("mylocker7.unlock() called.")
130  
131 class lockerex7(mylocker7):
132     @staticmethod
133     def acquire():
134         print("lockerex7.acquire() called.")
135          
136     @staticmethod
137     def unlock():
138         print("  lockerex7.unlock() called.")
139  
140 def lockhelper(cls):
141     '''cls 必须实现acquire和release静态方法'''
142     def _deco(func):
143         def __deco(*args, **kwargs):
144             print("before %s called." % func.__name__)
145             cls.acquire()
146             try:
147                 return func(*args, **kwargs)
148             finally:
149                 cls.unlock()
150         return __deco
151     return _deco
152  
153  
154 #from mylocker import *
155  
156 class example:
157     @lockhelper(mylocker7)
158     def myfunc(self):
159         print(" myfunc() called.")
160  
161     @lockhelper(mylocker7)
162     @lockhelper(lockerex7)
163     def myfunc2(self, a, b):
164         print(" myfunc2() called.")
165         return a + b
166 
167 
168 
169 if __name__ == '__main__':
170     print "---------------------"
171     #myfunc = deco(myfunc);
172     print myfunc()
173     print "---------------------"
174     print myfunc2()
175     print "---------------------"
176     print myfunc3(3, 4)
177     print "---------------------"
178     myfunc41(1, 2)
179     myfunc41(3, 4)
180     myfunc42(1, 2, 3)
181     myfunc42(3, 4, 5)
182     print "---------------------"
183     myfunc51()
184     myfunc52()
185     print "---------------------"
186     myfunc6()
187     myfunc6()
188     print "---------------------"
189     
190     a = example()
191     a.myfunc()
192     print(a.myfunc())
193     print(a.myfunc2(1, 2))
194     print(a.myfunc2(3, 4))
195     
196     pass
出自datakv
原文地址:https://www.cnblogs.com/datakv/p/5746656.html