少勞多得
Decorator 與 Python 之前引入的元編程抽象有著某些共同之處:即使沒有這些技術(shù),您也一樣可以實現(xiàn)它們所提供的功能。正如 Michele Simionato 和我在 可愛的 Python 專欄的早期文章 中指出的那樣,即使在 Python 1.5 中,也可以實現(xiàn) Python 類的創(chuàng)建,而不需要使用 “元類” 掛鉤。
Decorator 根本上的平庸與之非常類似。Decorator 所實現(xiàn)的功能就是修改緊接 Decorator 之后定義的函數(shù)和方法。這總是可能的,但這種功能主要是由 Python 2.2 中引入的 classmethod() 和 staticmethod() 內(nèi)置函數(shù)驅(qū)動的。在舊式風(fēng)格中,您可以調(diào)用 classmethod(),如下所示:
清單 1. 典型的 “舊式” classmethod
1
2
3
4
|
class C: def foo( cls , y): print "classmethod" , cls , y foo = classmethod (foo) |
雖然 classmethod() 是內(nèi)置函數(shù),但并無獨特之處;您也可以使用自己的方法轉(zhuǎn)換函數(shù)。例如:
清單 2. 典型的 “舊式” 方法的轉(zhuǎn)換
1
2
3
4
5
6
7
8
9
|
def enhanced(meth): def new( self , y): print "I am enhanced" return meth( self , y) return new class C: def bar( self , x): print "some method says:" , x bar = enhanced(bar) |
decorator 所做的一切就是使您避免重復(fù)使用方法名,并且將 decorator 放在方法定義中第一處提及其名稱的地方。例如:
清單 3. 典型的 “舊式” classmethod
1
2
3
4
5
6
7
|
class C: @classmethod def foo( cls , y): print "classmethod" , cls , y @enhanced def bar( self , x): print "some method says:" , x |
decorator 也可以用于正則函數(shù),采用的是與類中的方法相同的方式。令人驚奇的是,這一切是如此簡單(嚴(yán)格來說,甚至有些不必要),只需要對語法進行簡單修改,所有東西就可以工作得更好,并且使得程序的論證更加輕松。通過在方法定義的函數(shù)之前列出多個 decorator,即可將 decorator 鏈接在一起;良好的判斷可以有助于防止將過多 decorator 鏈接在一起,不過有時候?qū)讉€ decorator 鏈接在一起是有意義的:
清單 4. 鏈接 decorator
1
2
3
4
5
6
7
|
@synchronized @logging def myfunc(arg1, arg2, ...): # ...do something # decorators are equivalent to ending with: # myfunc = synchronized(logging(myfunc)) # Nested in that declaration order |
Decorator 只是一個語法糖,如果您過于急切,那么它就會使您搬起石頭砸了自己的腳。decorator 其實就是一個至少具有一個參數(shù)的函數(shù) —— 程序員要負責(zé)確保 decorator 的返回內(nèi)容仍然是一個有意義的函數(shù)或方法,并且實現(xiàn)了原函數(shù)為使連接有用而做的事情。例如,下面就是 decorator 兩個不正確的用法:
清單 5. 沒有返回函數(shù)的錯誤 decorator
1
2
3
4
5
6
7
8
9
10
11
12
|
>>> def spamdef(fn): ... print "spam, spam, spam" ... >>> @spamdef ... def useful(a, b): ... print a * * 2 + b * * 2 ... spam, spam, spam >>> useful( 3 , 4 ) Traceback (most recent call last): File "<stdin>" , line 1 , in ? TypeError: 'NoneType' object is not callable |
decorator 可能會返回一個函數(shù),但這個函數(shù)與未修飾的函數(shù)之間不存在有意義的關(guān)聯(lián):
清單 6. 忽略傳入函數(shù)的 decorator
1
2
3
4
5
6
7
8
9
10
11
|
>>> def spamrun(fn): ... def sayspam( * args): ... print "spam, spam, spam" ... return sayspam ... >>> @spamrun ... def useful(a, b): ... print a * * 2 + b * * 2 ... >>> useful( 3 , 4 ) spam, spam, spam |
最后,一個表現(xiàn)更良好的 decorator 可以在某些方面增強或修改未修飾函數(shù)的操作:
清單 7. 修改未修飾函數(shù)行為的 decorator
1
2
3
4
5
6
7
8
9
10
11
12
13
|
>>> def addspam(fn): ... def new( * args): ... print "spam, spam, spam" ... return fn( * args) ... return new ... >>> @addspam ... def useful(a, b): ... print a * * 2 + b * * 2 ... >>> useful( 3 , 4 ) spam, spam, spam 25 |
您可能會質(zhì)疑,useful() 到底有多么有用?addspam() 真的是那樣出色的增強 嗎?但這種機制至少符合您通常能在有用的 decorator 中看到的那種模式。
高級抽象簡介
根據(jù)我的經(jīng)驗,元類應(yīng)用最多的場合就是在類實例化之后對類中的方法進行修改。decorator 目前并不允許您修改類實例化本身,但是它們可以修改依附于類的方法。這并不能讓您在實例化過程中動態(tài)添加或刪除方法或類屬性,但是它讓這些方法可以在運行時根據(jù)環(huán)境的條件來變更其行為?,F(xiàn)在從技術(shù)上來說,decorator 是在運行 class 語句時應(yīng)用的,對于頂級類來說,它更接近于 “編譯時” 而非 “運行時”。但是安排 decorator 的運行時決策與創(chuàng)建類工廠一樣簡單。例如:
清單 8. 健壯但卻深度嵌套的 decorator
1
2
3
4
5
6
7
|
def arg_sayer(what): def what_sayer(meth): def new( self , * args, * * kws): print what return meth( self , * args, * * kws) return new return what_sayer |