淺析Python裝飾器以及裝飾器模式
漫談
如果作為一個(gè)Python入門(mén),不了解Python裝飾器也沒(méi)什么,但是如果作為一個(gè)中級(jí)Python開(kāi)發(fā)人員,如果再不對(duì)python裝飾器熟稔于心的話,那么可能并沒(méi)有量變積累到質(zhì)變。
我以前也看過(guò)很多講python 裝飾器的文章,但是都是看了就忘。一方面是沒(méi)有做太多的練習(xí),二是對(duì)它的領(lǐng)會(huì)不是很深。
希望引以為戒?。。?/p>
鄭傳
裝飾模式
如果你了解Java,你肯定聽(tīng)過(guò) 裝飾器模式。在面向?qū)ο笾?,裝飾模式指:動(dòng)態(tài)地給一個(gè)對(duì)象添加一些額外的職責(zé)。就增加一些功能來(lái)說(shuō),裝飾模式比生成子類更為靈活。
在設(shè)計(jì)模式學(xué)習(xí)----裝飾器模式,我摘取了下面一段使用裝飾器模式的代碼
public class DecoratorPattern { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here Basket basket = new Original(); //一個(gè)裝飾的過(guò)程 Basket myBasket =new AppleDecorator(new BananaDecorator(new OrangeDecorator(basket))); myBasket.show(); } }
等會(huì)注意下 Basket myBasket =new AppleDecorator(new BananaDecorator(new OrangeDecorator(basket))) 這段的寫(xiě)法
在Python官方文檔PythonDecorators 是這么介紹裝飾器的
What is a Decorator
A decorator is the name used for a software design pattern. Decorators dynamically alter the functionality of a function, method, or class without having to directly use subclasses or change the source code of the function being decorated.
翻一下: 就是裝飾器是一種軟件設(shè)計(jì)模式,被用來(lái)動(dòng)態(tài)修改函數(shù)、方法,或者類功能卻不是通過(guò)子類,或者修改原代碼實(shí)現(xiàn)。
跟之前是一個(gè)意思?。。?/p>
Python Decorator
而Python的裝飾器與之不同,官方這么說(shuō):
The "decorators" we talk about with concern to Python are not exactly the same thing as the DecoratorPattern described above. A Python decorator is a specific change to the Python syntax that allows us to more conveniently alter functions and methods (and possibly classes in a future version). This supports more readable applications of the DecoratorPattern but also other uses as well.
Support for the decorator syntax was proposed for Python in PEP 318, and will be implemented in Python 2.4.
翻譯下:Python的 decorators 與 DecoratorPattern并不完全相同。 Python的decorator是一種特殊:在語(yǔ)法上實(shí)現(xiàn)允許我們更靈活地更改方法,或者函數(shù)。
例子:
@classmethod def foo (arg1, arg2): ....
記住這個(gè)特殊的語(yǔ)法,后面我們會(huì)展示這個(gè)強(qiáng)大的語(yǔ)法糖
相關(guān)文章
pytorch 網(wǎng)絡(luò)參數(shù) weight bias 初始化詳解
這篇文章主要介紹了pytorch 網(wǎng)絡(luò)參數(shù) weight bias 初始化詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-06-06Python函數(shù)的返回值、匿名函數(shù)lambda、filter函數(shù)、map函數(shù)、reduce函數(shù)用法實(shí)例分析
這篇文章主要介紹了Python函數(shù)的返回值、匿名函數(shù)lambda、filter函數(shù)、map函數(shù)、reduce函數(shù)用法,結(jié)合實(shí)例形式分析了Python函數(shù)的返回值、匿名函數(shù)lambda、filter函數(shù)、map函數(shù)、reduce函數(shù)相關(guān)功能、原理與使用技巧,需要的朋友可以參考下2019-12-12Python利用CNN實(shí)現(xiàn)對(duì)時(shí)序數(shù)據(jù)進(jìn)行分類
這篇文章主要為大家詳細(xì)介紹了Python如何利用CNN實(shí)現(xiàn)對(duì)時(shí)序數(shù)據(jù)進(jìn)行分類功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2023-02-02淺談django rest jwt vue 跨域問(wèn)題
這篇文章主要介紹了django rest jwt vue 跨域問(wèn)題,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-10-10Django實(shí)現(xiàn)隨機(jī)圖形驗(yàn)證碼的示例
這篇文章主要介紹了Django實(shí)現(xiàn)隨機(jī)圖形驗(yàn)證碼的示例,幫助大家更好的學(xué)習(xí)和使用django框架,感興趣的朋友可以了解下2020-10-10python 實(shí)現(xiàn)批量替換文本中的某部分內(nèi)容
今天小編就為大家分享一篇python 實(shí)現(xiàn)批量替換文本中的某部分內(nèi)容,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-12-12python簡(jiǎn)單爬蟲(chóng)--get方式詳解
本篇文章介紹了python爬蟲(chóng)中g(shù)et和post方法介紹以及cookie作用,對(duì)此有興趣的朋友學(xué)習(xí)下,希望能夠給你帶來(lái)幫助2021-09-09