裝飾器的作用就是用一個(gè)新函數(shù)封裝舊函數(shù)(是舊函數(shù)代碼不變的情況下增加功能)然后會(huì)返回一個(gè)新函數(shù),新函數(shù)就叫做裝飾器,一般為了簡化裝飾器會(huì)用語法糖@新函數(shù)來簡化
例子:
這是一段代碼,但功能太少,要對(duì)這個(gè)進(jìn)行增強(qiáng),但又不能改變代碼。
【資料圖】
def hello(): return "hello world!"現(xiàn)在我們的需求是要增強(qiáng)hello()函數(shù)的功能,希望給返回加上HTML標(biāo)簽,比如hello world,但要求我們不得改變hello()函數(shù)原來的定義。
def makeitalic(fun):#makitalic傳了一個(gè)新函數(shù) def wrapped():#內(nèi)部函數(shù) return ""+fun()+""#要加的新功能 return wrapped#返回的是wrapped函數(shù)功能def hello():#對(duì)這個(gè)功能進(jìn)行增強(qiáng) return "hello world!"#makeitalic里面?zhèn)魅肓薶ello函數(shù),然后內(nèi)部函數(shù)fun()函數(shù)也就相當(dāng)于hello函數(shù)了hello_2=makeitalic(hello)#打印新函數(shù),返回的就是hello world!print(hello_2())為了增強(qiáng)原函數(shù)hello的功能,定義了一個(gè)函數(shù),它接收原函數(shù)作為參數(shù),并返回一個(gè)新的函數(shù),在這個(gè)返回的函數(shù)中,執(zhí)行了原函數(shù),并對(duì)原函數(shù)的功能進(jìn)行了增強(qiáng)。
事實(shí)上,makeitalic就是一個(gè)裝飾器(decorator),它封裝了原函數(shù)hello,并返回了一個(gè)新函數(shù),用于增強(qiáng)原函數(shù)的功能,并將其賦值給hello。
一般情況下,我們使用裝飾器提供的@語法糖(Syntactic Sugar),來簡化上面的操作。
####使用@語法糖def makeitalic(fun): def wrapped(): return "" + fun() + "" return wrapped@makeitalic#使用了裝飾器可以直接調(diào)用,不需要賦值了def hello(): return "hello world"print(hello())#使用了裝飾器可以直接調(diào)用,不需要賦值了像上面的情況,可以動(dòng)態(tài)的修改函數(shù)(或類的)功能的函數(shù)就是裝飾器。本質(zhì)上,它是一個(gè)高階函數(shù),以被裝飾的函數(shù)(比如上面的hello)為參數(shù),并返回一個(gè)包裝后的函數(shù)(比如上面的wrapped)給被修飾函數(shù)(hello)。
當(dāng)調(diào)用hello()函數(shù)時(shí),hello函數(shù)的執(zhí)行流程如下分析:
1.把hello函數(shù)作為參數(shù)傳給@符號(hào)后面的裝飾器函數(shù)。
2.然后開始執(zhí)行裝飾器函數(shù),并返回一個(gè)包裝了的函數(shù),同時(shí),改變?cè)瘮?shù)的指向,現(xiàn)在原函數(shù)指向了這個(gè)包裝函數(shù)。
3.執(zhí)行原函數(shù),其實(shí)此時(shí)執(zhí)行的是包裝了的函數(shù),所以說,裝飾器增強(qiáng)了一個(gè)現(xiàn)有函數(shù)的功能,但不會(huì)改變現(xiàn)有函數(shù)的定義。
普通裝飾器的使用形式:
@decoratordef fun(): pass#格式就如同下面的:#Python小白交流學(xué)習(xí)群:711312441def fun(): passfun = decorator(fun)#不使用語法糖要進(jìn)行賦值裝飾器可以定義多個(gè),離函數(shù)定義最近的裝飾器最先被調(diào)用,比如:
@decotator_one@decorator_twodef fun(): pass#格式如同下面的:def fun(): passfun = decorator_one(decorator_two(fun))裝飾器還可以帶參數(shù),比如:
@decorator(arg1, arg2)def fun(): pass#格式如同下面的:def fun(): passfun = decorator(arg1, arg2)(fun) 標(biāo)簽:


/4.jpg)



