# !/usr/bin/python
#-*- coding:utf-8 -*-
'''示例1: 最簡(jiǎn)單的函數(shù),表示調(diào)用了兩次'''
def myfun():
print('hello')
myfun()
myfun()
# ------------------------------------分割線--------------------------------------
# 使用裝飾函數(shù)在函數(shù)執(zhí)行前和執(zhí)行后分別附加額外功能
'''示例2: 替換函數(shù)(裝飾)
裝飾函數(shù)的參數(shù)是被裝飾的函數(shù)對(duì)象约郁,返回原函數(shù)對(duì)象
裝飾的實(shí)質(zhì)語(yǔ)句: myfunc = deco(myfunc)'''
def deco(func):
print('before')
func()
print('after')
return func
def myfunc():
print('myfunc called')
myfune = deco(myfunc)
myfune()
myfune()
# ------------------------------------分割線--------------------------------------
# 使用語(yǔ)法糖@來(lái)裝飾函數(shù)
'''示例3: 使用語(yǔ)法糖@來(lái)裝飾函數(shù)秕豫,相當(dāng)于“myfunc = deco(myfunc)”
但發(fā)現(xiàn)新函數(shù)只在第一次被調(diào)用危纫,且原函數(shù)多調(diào)用了一次'''
def deco(func):
print('before')
func()
print('later')
return func
@deco
def myfun():
print('myfun,,,,')
myfun()
myfun()
# ------------------------------------分割線--------------------------------------
# 使用內(nèi)嵌包裝函數(shù)來(lái)確保每次新函數(shù)都被調(diào)用
'''示例4: 使用內(nèi)嵌包裝函數(shù)來(lái)確保每次新函數(shù)都被調(diào)用渔欢,
內(nèi)嵌包裝函數(shù)的形參和返回值與原函數(shù)相同状蜗,裝飾函數(shù)返回內(nèi)嵌包裝函數(shù)對(duì)象'''
def deco(abc):
def _deco():
print('before...')
abc()
print('later...')
return _deco
@deco
def cc():
print('cc....')
cc()
cc()
# ------------------------------------分割線--------------------------------------
# 對(duì)帶參數(shù)的函數(shù)進(jìn)行裝飾
'''示例5: 對(duì)帶參數(shù)的函數(shù)進(jìn)行裝飾丧裁,
內(nèi)嵌包裝函數(shù)的形參和返回值與原函數(shù)相同掉蔬,裝飾函數(shù)返回內(nèi)嵌包裝函數(shù)對(duì)象'''
def deco(func):
def _deco(a,b):
print('before')
func(a,b)
print('after')
return _deco
@deco
def myfun(a,b):
print('my,,,,')
return a+b
myfun(1,2)
myfun(3,4)
# ------------------------------------分割線--------------------------------------
# 對(duì)參數(shù)數(shù)量不確定的函數(shù)進(jìn)行裝飾
'''示例6: 對(duì)參數(shù)數(shù)量不確定的函數(shù)進(jìn)行裝飾勾习,
參數(shù)用(*args, **kwargs)浓瞪,自動(dòng)適應(yīng)變參和命名參數(shù)'''
def deco(func):
def _deco(*args,**kwargs):
print('before')
func(*args,**kwargs)
print('after')
return _deco
@deco
def myfun1(a,b):
print('myfun')
return a+b
@deco
def myfun2(a,b,c):
print('fun2...')
return a+b+c
myfun1(1,2)
myfun2(1,2,3)
# ------------------------------------分割線--------------------------------------
# 讓裝飾器帶參數(shù)
# '''示例7: 在示例4的基礎(chǔ)上,讓裝飾器帶參數(shù)巧婶,
# 和上一示例相比在外層多了一層包裝乾颁。
# 裝飾函數(shù)名實(shí)際上應(yīng)更有意義些'''
def deco(arg):
def _deco(func):
def __deco():
print('before')
func()
print('after')
return __deco
return _deco
@deco('moudle1')
def myfun():
print('myfun,,,')
@deco('moudle2')
def myfun2():
print('myfun222')
myfun()
myfun2()
# ------------------------------------分割線--------------------------------------
# 讓裝飾器帶 類 參數(shù)
'''示例8: 裝飾器帶類參數(shù)'''
class locker:
def __init__():
print('locker __init__.........')
@staticmethod
def acquire():
print('locker.acquire..... ')
@staticmethod
def release():
print('locker.release...........')
def deco(cls):
def _deco(func):
def __deco():
print('bofore.....')
cls.acquire()
try:
return func
finally:
cls.release()
return __deco
return _deco
@deco(locker)
def myfunc():
print('myfunc.....')
myfunc()
myfunc()
# ------------------------------------分割線--------------------------------------
# 裝飾器帶類參數(shù)涂乌,并分拆公共類到其他py文件中,同時(shí)演示了對(duì)一個(gè)函數(shù)應(yīng)用多個(gè)裝飾器
'''mylocker.py: 公共類 for 示例9.py'''
class mylocker:
def __init__(self):
print('__init__called')
@staticmethod
def acquire():
print('mylocker.acquire.......')
@staticmethod
def unlock():
print('mylocker.unlock..........')
class lockerex(mylocker):
@staticmethod
def acquire():
print('lockerex.acquire')
@staticmethod
def unlock():
print('lockerex.unlock.....')
def lockhelper(cls):
'''cls 必須實(shí)現(xiàn)acquire和release靜態(tài)方法'''
def _deco(func):
def __deco(*args,**kwargs):
print('before..........')
cls.acquire()
try:
return func(*args,**kwargs)
finally:
cls.unlock()
return __deco
return _deco
# 調(diào)用
class example:
@lockhelper(mylocker)
def myfunc(self):
print('example.myfunc...........')
@lockhelper(mylocker)
@lockhelper(lockerex)
def myfun2(self,a,b):
print('example.myfun2.......')
return a+b
if __name__ == "__main__":
a = example()
a.myfunc()
print(a.myfunc())
print(a.myfun2(1,2))
print(a.myfun2(3,4))
*僅學(xué)習(xí)英岭,侵刪