新聞中心
本文轉(zhuǎn)載自微信公眾號(hào)「dongfanger」,作者dongfanger。轉(zhuǎn)載本文請(qǐng)聯(lián)系dongfanger公眾號(hào)。

創(chuàng)新互聯(lián)建站是一家專業(yè)提供宜黃企業(yè)網(wǎng)站建設(shè),專注與做網(wǎng)站、網(wǎng)站制作、H5網(wǎng)站設(shè)計(jì)、小程序制作等業(yè)務(wù)。10年已為宜黃眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站制作公司優(yōu)惠進(jìn)行中。
函數(shù)裝飾器是Python語(yǔ)言最優(yōu)秀的設(shè)計(jì)之一,它以非常簡(jiǎn)潔的方式增強(qiáng)了函數(shù)的行為,讓崎嶇不平之路變得平坦順暢。
函數(shù)裝飾器是什么
函數(shù)裝飾器是一個(gè)可調(diào)用對(duì)象,它的參數(shù)是另外一個(gè)函數(shù)。比如:
- @decorate
- def target():
- print("running target()")
跟下面代碼效果是一樣的:
- def target():
- print("running target()")
- target = decorate(target)
簡(jiǎn)單實(shí)現(xiàn)@decorate:
- def decorate(func):
- def inner():
- print("running inner()")
- return inner
測(cè)試一下:
- >>> target()
- running inner()
- >>> target
.inner at 0x04899D18>
新的target是decorate(target)返回的inner函數(shù)。
因?yàn)檠b飾器只是代碼優(yōu)化的一種手段,不像if語(yǔ)句for語(yǔ)句那樣,決定了程序流程,所以嚴(yán)格來(lái)說(shuō),裝飾器只是語(yǔ)法糖。它有兩個(gè)特性,一是能把被裝飾的函數(shù)替換成其他函數(shù),二是裝飾器在加載模塊時(shí)立即執(zhí)行。
裝飾器在導(dǎo)入時(shí)執(zhí)行
若想真正理解裝飾器,需要區(qū)分導(dǎo)入時(shí)和運(yùn)行時(shí)。函數(shù)裝飾器在導(dǎo)入模塊時(shí)立即執(zhí)行,而被裝飾的函數(shù)只在明確調(diào)用時(shí)運(yùn)行。
接下來(lái)通過(guò)示例對(duì)這個(gè)特性進(jìn)行說(shuō)明,新建registration.py模塊:
- registry = []
- def register(func):
- # 裝飾器函數(shù)也可以不定義內(nèi)部函數(shù)
- print("running register(%s)" % func)
- registry.append(func)
- return func
- @register
- def f1():
- print("running f1()")
- @register
- def f2():
- print("running f2()")
- def f3():
- print("running f3()")
- def main():
- print("running main()")
- print("registry ->", registry)
- f1()
- f2()
- f3()
- if __name__ == "__main__":
- main()
從結(jié)果能看出來(lái):
- @register作用到f1和f2上,在導(dǎo)入時(shí),在main()調(diào)用前就執(zhí)行了。
- f3沒(méi)有裝飾器,就沒(méi)有在main()調(diào)用前執(zhí)行@register。
- 在main()調(diào)用后,明確調(diào)用f1()、f2()、f3()才執(zhí)行函數(shù)。
import模塊能看得更明顯:
- >>> import registration
- running register(
) - running register(
)
裝飾器在導(dǎo)入時(shí)就執(zhí)行了。
使用裝飾器改進(jìn)策略模式
在《Python設(shè)計(jì)模式知多少》文章中提到了裝飾器可以更優(yōu)雅的實(shí)現(xiàn)策略模式的最佳策略,它的實(shí)現(xiàn)代碼如下:
- promos = []
- def promotion(promo_func):
- promos.append(promo_func)
- return promo_func
- @promotion
- def fidelity(order):
- """5% discount for customers with 1000 or more fidelity points"""
- return order.total() * .05 if order.customer.fidelity >= 1000 else 0
- @promotion
- def bulk_item(order):
- """10% discount for each LineItem with 20 or more units"""
- discount = 0
- for item in order.cart:
- if item.quantity >= 20:
- discount += item.total() * .1
- return discount
- @promotion
- def large_order(order):
- """7% discount for orders with 10 or more distinct items"""
- distinct_items = {item.product for item in order.cart}
- if len(distinct_items) >= 10:
- return order.total() * .07
- return 0
- def best_promo(order):
- """Select best discount available
- """
- return max(promo(order) for promo in promos)
它解決了"如果想要添加新的促銷策略,那么要定義相應(yīng)函數(shù)并添加到promos列表中"這個(gè)缺陷,并有更多優(yōu)點(diǎn):
- 新的促銷策略,用@promotion裝飾器即可添加。
- 促銷策略函數(shù)不用以_promo結(jié)尾,可以任意命令。
- 促銷策略可以在任意模塊定義,只需要使用@promotion裝飾器即可。
小結(jié)
本文首先介紹了函數(shù)裝飾器是一個(gè)可調(diào)用對(duì)象,它的參數(shù)是另外一個(gè)函數(shù)。嚴(yán)格來(lái)說(shuō),它只是語(yǔ)法糖。要理解裝飾器,需要區(qū)別導(dǎo)入時(shí)和運(yùn)行時(shí),裝飾器在導(dǎo)入時(shí)就會(huì)執(zhí)行。最后使用裝飾器對(duì)策略模式的最佳策略進(jìn)行了優(yōu)化。為了進(jìn)一步學(xué)習(xí)函數(shù)裝飾器,得先明白另外一個(gè)很重要的概念:閉包。
參考資料:
《流暢的Python》
分享題目:一篇學(xué)會(huì)Python函數(shù)裝飾器基礎(chǔ)知識(shí)
URL標(biāo)題:http://fisionsoft.com.cn/article/dpcjcge.html


咨詢
建站咨詢
