少勞多得
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
class C: def foo(cls, y): print "classmethod", cls, y foo = classmethod(foo)
雖然 classmethod() 是內(nèi)置函數(shù),但并無獨特之處;您也可以使用自己的方法轉(zhuǎn)換函數(shù)。例如:
清單 2. 典型的 “舊式” 方法的轉(zhuǎn)換
def enhanced(meth): def new(self, y): print "I am enhanced" return meth(self, y) return newclass C: def bar(self, x): print "some method says:", x bar = enhanced(bar)
decorator 所做的一切就是使您避免重復(fù)使用方法名,并且將 decorator 放在方法定義中第一處提及其名稱的地方。例如:
清單 3. 典型的 “舊式” classmethod
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)格來說,甚至有些不必要),只需要對語法進(jìn)行簡單修改,所有東西就可以工作得更好,并且使得程序的論證更加輕松。通過在方法定義的函數(shù)之前列出多個 decorator,即可將 decorator 鏈接在一起;良好的判斷可以有助于防止將過多 decorator 鏈接在一起,不過有時候?qū)讉€ decorator 鏈接在一起是有意義的:
清單 4. 鏈接 decorator
@synchronized@loggingdef myfunc(arg1, arg2, ...): # ...do something# decorators are equivalent to ending with:# myfunc = synchronized(logging(myfunc))# Nested in that declaration order
Decorator 只是一個語法糖,如果您過于急切,那么它就會使您搬起石頭砸了自己的腳。decorator 其實就是一個至少具有一個參數(shù)的函數(shù) —— 程序員要負(fù)責(zé)確保 decorator 的返回內(nèi)容仍然是一個有意義的函數(shù)或方法,并且實現(xiàn)了原函數(shù)為使連接有用而做的事情。例如,下面就是 decorator 兩個不正確的用法:
清單 5. 沒有返回函數(shù)的錯誤 decorator
>>> 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
新聞熱點
疑難解答
圖片精選