1.在執行目標函數前附加一些內容或者功能:python
def demo(func): print('before exec %s '%func.__name__) func() print('after exec %s '%func.__name__) return func def func(): print('hello world') func = demo(func) func()
2.使用語法糖@來裝飾函數app
def demo(func): print('before exec %s '%func.__name__) func() print('after exec %s '%func.__name__) return func @demo def func(): print('hello world') func()
3.使用內嵌包裝飾函數保證每次新函數都被調用函數
def demo(func): def inner(): print('before exec %s '%func.__name__) func() print('after exec %s '%func.__name__) return inner @demo def func(): print('hello world') func()
4.對帶參數的函數進行裝飾ui
def demo(func): def inner(a,b): print('before exec %s '%func.__name__) ret = func(a,b) print('after exec %s '%func.__name__) return ret return inner @demo def func(a,b): print('hello world') return a+b print(func(1,2))
5.對參數數量不肯定的函數進行裝飾對象
def demo(func): def inner(*args,**kwargs): print('before exec %s '%func.__name__) ret = func(*args,**kwargs) print('after exec %s '%func.__name__) return ret return inner @demo def func(a,b): print('hello world func') return a+b @demo def func1(a,b,c): print('hello world func1') return a+b+c print(func(1,2)) print(func1(1,2,3))
6.裝飾器帶參數blog
def demo(arg): def warper(func): def inner(*args,**kwargs): print('before exec %s %s'%(func.__name__,arg)) ret = func(*args,**kwargs) print('after exec %s %s '%(func.__name__,arg)) return ret return inner return warper @demo('qq') def func(a,b): print('hello world func') return a+b @demo('wechat') def func1(a,b,c): print('hello world func1') return a+b+c print(func(1,2)) print(func1(1,2,3))
7.多個裝飾器裝飾一個函數it
def wrapper1(func): def inner(): print('wrapper1 ,before func') func() print('wrapper1 ,after func') return inner def wrapper2(func): def inner(): print('wrapper2 ,before func') func() print('wrapper2 ,after func') return inner @wrapper2 @wrapper1 def f(): print('in f') f()
8.裝飾器帶類參數class
'''''示例: 裝飾器帶類參數''' 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()
9.裝飾器帶類參數,並分拆公共類到其餘py文件中,同時演示了對一個函數應用多個裝飾器import
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
'''''裝飾器帶類參數,並分拆公共類到其餘py文件中 同時演示了對一個函數應用多個裝飾器''' from mylocker import * 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))