如何正確理解python裝飾器
要想了解裝飾器,首先要了解一個(gè)概念,閉包。什么是閉包,一句話說(shuō)就是,在函數(shù)中再嵌套一個(gè)函數(shù),并且引用外部函數(shù)的變量,這就是一個(gè)閉包了。光說(shuō)沒(méi)有概念,直接上一個(gè)例子。
def outer(x): def inner(y):return x + y return innerprint(outer(6)(5))----------------------------->>>11
如代碼所示,在outer函數(shù)內(nèi),又定義了一個(gè)inner函數(shù),并且inner函數(shù)又引用了外部函數(shù)outer的變量x,這就是一個(gè)閉包了。在輸出時(shí),outer(6)(5),第一個(gè)括號(hào)傳進(jìn)去的值返回inner函數(shù),其實(shí)就是返回6 + y,所以再傳第二個(gè)參數(shù)進(jìn)去,就可以得到返回值,6 + 5。
二、裝飾器接下來(lái)就講裝飾器,其實(shí)裝飾器就是一個(gè)閉包,裝飾器是閉包的一種應(yīng)用。什么是裝飾器呢,簡(jiǎn)言之,python裝飾器就是用于拓展原來(lái)函數(shù)功能的一種函數(shù),這個(gè)函數(shù)的特殊之處在于它的返回值也是一個(gè)函數(shù),使用python裝飾器的好處就是在不用更改原函數(shù)的代碼前提下給函數(shù)增加新的功能。使用時(shí),再需要的函數(shù)前加上@demo即可。
def debug(func): def wrapper():print('[DEBUG]: enter {}()'.format(func.__name__))return func() return wrapper@debugdef hello(): print('hello')hello()----------------------------->>>[DEBUG]: enter hello()>>>hello
例子中的裝飾器給函數(shù)加上一個(gè)進(jìn)入函數(shù)的debug模式,不用修改原函數(shù)代碼就完成了這個(gè)功能,可以說(shuō)是很方便了。
三、帶參數(shù)的裝飾器上面例子中的裝飾器是不是功能太簡(jiǎn)單了,那么裝飾器可以加一些參數(shù)嗎,當(dāng)然是可以的,另外裝飾的函數(shù)當(dāng)然也是可以傳參數(shù)的。
def logging(level): def outwrapper(func):def wrapper(*args, **kwargs): print('[{0}]: enter {1}()'.format(level, func.__name__)) return func(*args, **kwargs)return wrapper return outwrapper@logging(level='INFO')def hello(a, b, c): print(a, b, c)hello('hello,','good','morning')----------------------------->>>[INFO]: enter hello()>>>hello, good morning
如上,裝飾器中可以傳入?yún)?shù),先形成一個(gè)完整的裝飾器,然后再來(lái)裝飾函數(shù),當(dāng)然函數(shù)如果需要傳入?yún)?shù)也是可以的,用不定長(zhǎng)參數(shù)符號(hào)就可以接收,例子中傳入了三個(gè)參數(shù)。
四、類裝飾器裝飾器也不一定只能用函數(shù)來(lái)寫(xiě),也可以使用類裝飾器,用法與函數(shù)裝飾器并沒(méi)有太大區(qū)別,實(shí)質(zhì)是使用了類方法中的__call__魔法方法來(lái)實(shí)現(xiàn)類的直接調(diào)用。
class logging(object): def __init__(self, func):self.func = func def __call__(self, *args, **kwargs):print('[DEBUG]: enter {}()'.format(self.func.__name__))return self.func(*args, **kwargs)@loggingdef hello(a, b, c): print(a, b, c)hello('hello,','good','morning')----------------------------->>>[DEBUG]: enter hello()>>>hello, good morning
類裝飾器也是可以帶參數(shù)的,如下實(shí)現(xiàn)
class logging(object): def __init__(self, level):self.level = level def __call__(self, func):def wrapper(*args, **kwargs): print('[{0}]: enter {1}()'.format(self.level, func.__name__)) return func(*args, **kwargs)return wrapper@logging(level='TEST')def hello(a, b, c): print(a, b, c)hello('hello,','good','morning')----------------------------->>>[TEST]: enter hello()>>>hello, good morning
好了,如上就是裝飾器的一些概念和大致的用法啦,想更深入的了解裝飾器還是需要自己在平時(shí)的練習(xí)和應(yīng)用中多體會(huì)
以上就是如何正確理解python裝飾器的詳細(xì)內(nèi)容,更多關(guān)于python裝飾器的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. html清除浮動(dòng)的6種方法示例2. PHP設(shè)計(jì)模式中工廠模式深入詳解3. 解析原生JS getComputedStyle4. PHP循環(huán)與分支知識(shí)點(diǎn)梳理5. 匹配模式 - XSL教程 - 46. 輕松學(xué)習(xí)XML教程7. 將properties文件的配置設(shè)置為整個(gè)Web應(yīng)用的全局變量實(shí)現(xiàn)方法8. CSS3實(shí)例分享之多重背景的實(shí)現(xiàn)(Multiple backgrounds)9. HTML5 Canvas繪制圖形從入門(mén)到精通10. chat.asp聊天程序的編寫(xiě)方法
