pythonアクセサリー学習



   :      ,        

# -*- coding:gbk -*-
'''  1:       ,       '''
 
def myfunc():
    print("myfunc() called.")
 
myfunc()
myfunc()
 

   :                        


# -*- coding:gbk -*-
'''  2:     (  )
                ,       
       : myfunc = deco(myfunc)'''
 
def deco(func):
    print("before myfunc() called.")
    func()
    print("  after myfunc() called.")
    return func
 
def myfunc():
    print(" myfunc() called.")
 
myfunc= deco(myfunc)
 
myfunc()
myfunc()

   :     @     


# -*- coding:gbk -*-
'''  3:      @     ,   “myfunc = deco(myfunc)”
              ,          '''
 
def deco(func):
    print("before myfunc() called.")
    func()
    print("  after myfunc() called.")
    return func
 
@deco
def myfunc():
    print(" myfunc() called.")
 
myfunc()
myfunc()

   :                    


# -*- coding:gbk -*-
'''  4:                     ,
                   ,              '''
 
def deco(func):
    def _deco():
        print("before myfunc() called.")
        func()
        print("  after myfunc() called.")
        #      func,             
    return _deco
 
@deco
def myfunc():
    print(" myfunc() called.")
    return 'ok'
 
myfunc()
myfunc()

   :           


# -*- coding:gbk -*-
'''  5:            ,
                   ,              '''
 
def deco(func):
    def _deco(a, b):
        print("before myfunc() called.")
        ret= func(a, b)
        print("  after myfunc() called. result: %s" % ret)
        return ret
    return _deco
 
@deco
def myfunc(a, b):
    print(" myfunc(%s,%s) called." % (a, b))
    return a+ b
 
myfunc(1,2)
myfunc(3,4)

   :               


# -*- coding:gbk -*-
'''  6:                ,
   (*args, **kwargs),           '''
 
def deco(func):
    def _deco(*args,**kwargs):
        print("before %s called." % func.__name__)
        ret= func(*args,**kwargs)
        print("  after %s called. result: %s" % (func.__name__, ret))
        return ret
    return _deco
 
@deco
def myfunc(a, b):
    print(" myfunc(%s,%s) called." % (a, b))
    return a+b
 
@deco
def myfunc2(a, b, c):
    print(" myfunc2(%s,%s,%s) called." % (a, b, c))
    return a+b+c
 
myfunc(1,2)
myfunc(3,4)
myfunc2(1,2,3)
myfunc2(3,4,5)

   :       


# -*- coding:gbk -*-
'''  7:    4    ,       ,
                。
              '''
 
def deco(arg):
    def _deco(func):
        def __deco():
            print("before %s called [%s]." % (func.__name__, arg))
            func()
            print("  after %s called [%s]." % (func.__name__, arg))
        return __deco
    return _deco
 
@deco("mymodule")
def myfunc():
    print(" myfunc() called.")
 
@deco("module2")
def myfunc2():
    print(" myfunc2() called.")
 
myfunc()
myfunc2()

   :          


# -*- coding:gbk -*-
'''  8:        '''
 
class locker:
    def __init__(self):
        print("locker.__init__() should be not called.")
         
    @staticmethod
    def acquire():
        print("locker.acquire() called.(      )")
         
    @staticmethod
    def release():
        print("  locker.release() called.(       )")
 
def deco(cls):
    '''cls     acquire release    '''
    def _deco(func):
        def __deco():
            print("before %s called [%s]." % (func.__name__,cls))
            cls.acquire()
            try:
                return func()
            finally:
                cls.release()
        return __deco
    return _deco
 
@deco(locker)
def myfunc():
    print(" myfunc() called.")
 
myfunc()
myfunc()

   :       ,         py   ,                 


# -*- coding:gbk -*-
'''mylocker.py:     for   9.py'''
 
class mylocker:
    def __init__(self):
        print("mylocker.__init__() called.")
         
    @staticmethod
    def acquire():
        print("mylocker.acquire() called.")
         
    @staticmethod
    def unlock():
        print("  mylocker.unlock() called.")
 
class lockerex(mylocker):
    @staticmethod
    def acquire():
        print("lockerex.acquire() called.")
         
    @staticmethod
    def unlock():
        print("  lockerex.unlock() called.")
 
def lockhelper(cls):
    '''cls     acquire release    '''
    def _deco(func):
        def __deco(*args,**kwargs):
            print("before %s called." % func.__name__)
            cls.acquire()
            try:
                return func(*args,**kwargs)
            finally:
                cls.unlock()
        return __deco
    return _deco


# -*- coding:gbk -*-
'''  9:        ,         py   
                 '''
 
from mylockerimport *
 
class example:
    @lockhelper(mylocker)
    def myfunc(self):
        print(" myfunc() called.")
 
    @lockhelper(mylocker)
    @lockhelper(lockerex)
    def myfunc2(self, a, b):
        print(" myfunc2() called.")
        return a+ b
 
if __name__=="__main__":
    a= example()
    a.myfunc()
    print(a.myfunc())
    print(a.myfunc2(1,2))
    print(a.myfunc2(3,4))

       ,           ,         :

1. Python      http://blog.csdn.net/thy38/article/details/4471421

2. Python           http://www.cnblogs.com/huxi/archive/2011/03/01/1967600.html

3. Python       http://apps.hi.baidu.com/share/detail/17572338