Python裝飾器入門學(xué)習(xí)教程(九步學(xué)習(xí))
裝飾器(decorator)是一種高級(jí)Python語(yǔ)法。裝飾器可以對(duì)一個(gè)函數(shù)、方法或者類進(jìn)行加工。在Python中,我們有多種方法對(duì)函數(shù)和類進(jìn)行加工,比如在Python閉包中,我們見(jiàn)到函數(shù)對(duì)象作為某一個(gè)函數(shù)的返回結(jié)果。相對(duì)于其它方式,裝飾器語(yǔ)法簡(jiǎn)單,代碼可讀性高。因此,裝飾器在Python項(xiàng)目中有廣泛的應(yīng)用。
這是在Python學(xué)習(xí)小組上介紹的內(nèi)容,現(xiàn)學(xué)現(xiàn)賣、多練習(xí)是好的學(xué)習(xí)方式。
第一步:最簡(jiǎn)單的函數(shù),準(zhǔn)備附加額外功能
# -*- coding:gbk -*-
'''示例1: 最簡(jiǎn)單的函數(shù),表示調(diào)用了兩次'''
def myfunc():
print("myfunc() called.")
myfunc()
myfunc()
第二步:使用裝飾函數(shù)在函數(shù)執(zhí)行前和執(zhí)行后分別附加額外功能
# -*- coding:gbk -*-
'''示例2: 替換函數(shù)(裝飾)
裝飾函數(shù)的參數(shù)是被裝飾的函數(shù)對(duì)象,返回原函數(shù)對(duì)象
裝飾的實(shí)質(zhì)語(yǔ)句: 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()
第三步:使用語(yǔ)法糖@來(lái)裝飾函數(shù)
# -*- coding:gbk -*-
'''示例3: 使用語(yǔ)法糖@來(lái)裝飾函數(shù),相當(dāng)于“myfunc = deco(myfunc)”
但發(fā)現(xiàn)新函數(shù)只在第一次被調(diào)用,且原函數(shù)多調(diào)用了一次'''
def deco(func):
print("before myfunc() called.")
func()
print(" after myfunc() called.")
return func
@deco
def myfunc():
print(" myfunc() called.")
myfunc()
myfunc()
第四步:使用內(nèi)嵌包裝函數(shù)來(lái)確保每次新函數(shù)都被調(diào)用
# -*- coding:gbk -*-
'''示例4: 使用內(nèi)嵌包裝函數(shù)來(lái)確保每次新函數(shù)都被調(diào)用,
內(nèi)嵌包裝函數(shù)的形參和返回值與原函數(shù)相同,裝飾函數(shù)返回內(nèi)嵌包裝函數(shù)對(duì)象'''
def deco(func):
def _deco():
print("before myfunc() called.")
func()
print(" after myfunc() called.")
# 不需要返回func,實(shí)際上應(yīng)返回原函數(shù)的返回值
return _deco
@deco
def myfunc():
print(" myfunc() called.")
return 'ok'
myfunc()
myfunc()
第五步:對(duì)帶參數(shù)的函數(shù)進(jìn)行裝飾
# -*- coding:gbk -*-
'''示例5: 對(duì)帶參數(shù)的函數(shù)進(jìn)行裝飾,
內(nèi)嵌包裝函數(shù)的形參和返回值與原函數(shù)相同,裝飾函數(shù)返回內(nèi)嵌包裝函數(shù)對(duì)象'''
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)
第六步:對(duì)參數(shù)量不確定的函數(shù)進(jìn)行裝飾
# -*- coding:gbk -*-
'''示例6: 對(duì)參數(shù)數(shù)量不確定的函數(shù)進(jìn)行裝飾,
參數(shù)用(*args, **kwargs),自動(dòng)適應(yīng)變參和命名參數(shù)'''
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)
第七步:讓裝飾器帶參數(shù)
# -*- coding:gbk -*-
'''示例7: 在示例4的基礎(chǔ)上,讓裝飾器帶參數(shù),
和上一示例相比在外層多了一層包裝。
裝飾函數(shù)名實(shí)際上應(yīng)更有意義些'''
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()
第八步:讓裝飾器帶 類 參數(shù)
# -*- coding:gbk -*-
'''示例8: 裝飾器帶類參數(shù)'''
class locker:
def __init__(self):
print("locker.__init__() should be not called.")
@staticmethod
def acquire():
print("locker.acquire() called.(這是靜態(tài)方法)")
@staticmethod
def release():
print(" locker.release() called.(不需要對(duì)象實(shí)例)")
def deco(cls):
'''cls 必須實(shí)現(xiàn)acquire和release靜態(tài)方法'''
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()
第九步:裝飾器帶類參數(shù),并分拆公共類到其他py文件中,同時(shí)演示了對(duì)一個(gè)函數(shù)應(yīng)用多個(gè)裝飾器
# -*- 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 必須實(shí)現(xiàn)acquire和release靜態(tài)方法'''
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: 裝飾器帶類參數(shù),并分拆公共類到其他py文件中
同時(shí)演示了對(duì)一個(gè)函數(shù)應(yīng)用多個(gè)裝飾器'''
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))
以上給大家分享了Python裝飾器入門學(xué)習(xí)教程(九步學(xué)習(xí)),希望對(duì)大家有所幫助。
- 實(shí)例講解Python編程中@property裝飾器的用法
- Python中使用裝飾器來(lái)優(yōu)化尾遞歸的示例
- Python黑魔法@property裝飾器的使用技巧解析
- python中函數(shù)總結(jié)之裝飾器閉包詳解
- 深入理解python中的閉包和裝飾器
- Python的幾個(gè)高級(jí)語(yǔ)法概念淺析(lambda表達(dá)式閉包裝飾器)
- 淺析Python編寫函數(shù)裝飾器
- Python裝飾器基礎(chǔ)詳解
- 分析Python中設(shè)計(jì)模式之Decorator裝飾器模式的要點(diǎn)
- python利用裝飾器進(jìn)行運(yùn)算的實(shí)例分析
- 簡(jiǎn)單上手Python中裝飾器的使用
- 深入學(xué)習(xí)Python中的裝飾器使用
相關(guān)文章
python抓取京東價(jià)格分析京東商品價(jià)格走勢(shì)
本文介紹使用python抓取京東價(jià)格的代碼,用于分析京東商品價(jià)格走勢(shì)或者用于其它,大家參考使用吧2014-01-01
Python數(shù)據(jù)結(jié)構(gòu)之樹(shù)的全面解讀
數(shù)據(jù)結(jié)構(gòu)中有很多樹(shù)的結(jié)構(gòu),其中包括二叉樹(shù)、二叉搜索樹(shù)、2-3樹(shù)、紅黑樹(shù)等等。本文中對(duì)數(shù)據(jù)結(jié)構(gòu)中常見(jiàn)的樹(shù)邏輯結(jié)構(gòu)和存儲(chǔ)結(jié)構(gòu)進(jìn)行了匯總,不求嚴(yán)格精準(zhǔn),但求簡(jiǎn)單易懂2021-11-11
Pycharm中Python環(huán)境配置常見(jiàn)問(wèn)題解析
這篇文章主要介紹了Pycharm中Python環(huán)境配置常見(jiàn)問(wèn)題,結(jié)合圖文形式分析了Pycharm中Python環(huán)境配置模塊路徑問(wèn)題、虛擬環(huán)境創(chuàng)建、配置遠(yuǎn)程服務(wù)器、連接數(shù)據(jù)庫(kù)等常見(jiàn)問(wèn)題與操作方法,需要的朋友可以參考下2020-01-01
django創(chuàng)建超級(jí)用戶時(shí)指定添加其它字段方式
這篇文章主要介紹了django創(chuàng)建超級(jí)用戶時(shí)指定添加其它字段方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-05-05
Python?TypeError:?‘float‘?object?is?not?subscriptable錯(cuò)
發(fā)現(xiàn)問(wèn)題寫python的時(shí)候出現(xiàn)了這個(gè)錯(cuò),所以想著給大家總結(jié)下,這篇文章主要給大家介紹了關(guān)于Python?TypeError:?‘float‘?object?is?not?subscriptable錯(cuò)誤的解決辦法,需要的朋友可以參考下2022-12-12
python讀取tif圖片時(shí)保留其16bit的編碼格式實(shí)例
今天小編就為大家分享一篇python讀取tif圖片時(shí)保留其16bit的編碼格式實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-01-01
python 實(shí)現(xiàn)"神經(jīng)衰弱"翻牌游戲
這篇文章主要介紹了python 實(shí)現(xiàn)"神經(jīng)衰弱"游戲,幫助大家更好的理解和使用python的pygame庫(kù),感興趣的朋友可以了解下2020-11-11
詳解python關(guān)于多級(jí)包之間的引用問(wèn)題
本文主要介紹了python關(guān)于多級(jí)包之間的引用問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-08-08

