日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
一篇學(xué)會(huì)Python函數(shù)裝飾器基礎(chǔ)知識(shí)

本文轉(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ōu)秀的設(shè)計(jì)之一,它以非常簡(jiǎn)潔的方式增強(qiáng)了函數(shù)的行為,讓崎嶇不平之路變得平坦順暢。

函數(shù)裝飾器是什么

函數(shù)裝飾器是一個(gè)可調(diào)用對(duì)象,它的參數(shù)是另外一個(gè)函數(shù)。比如:

 
 
 
 
  1. @decorate
  2. def target():
  3.     print("running target()")

跟下面代碼效果是一樣的:

 
 
 
 
  1. def target():
  2.     print("running target()")
  3. target = decorate(target)

簡(jiǎn)單實(shí)現(xiàn)@decorate:

 
 
 
 
  1. def decorate(func):
  2.     def inner():
  3.         print("running inner()")
  4.     return inner

測(cè)試一下:

 
 
 
 
  1. >>> target()
  2. running inner()
  3. >>> target
  4. .inner at 0x04899D18>

新的target是decorate(target)返回的inner函數(shù)。

因?yàn)檠b飾器只是代碼優(yōu)化的一種手段,不像if語句for語句那樣,決定了程序流程,所以嚴(yán)格來說,裝飾器只是語法糖。它有兩個(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)行。

接下來通過示例對(duì)這個(gè)特性進(jìn)行說明,新建registration.py模塊:

 
 
 
 
  1. registry = []
  2. def register(func):
  3.     # 裝飾器函數(shù)也可以不定義內(nèi)部函數(shù)
  4.     print("running register(%s)" % func)
  5.     registry.append(func)
  6.     return func
  7. @register
  8. def f1():
  9.     print("running f1()")
  10. @register
  11. def f2():
  12.     print("running f2()")
  13. def f3():
  14.     print("running f3()")
  15. def main():
  16.     print("running main()")
  17.     print("registry ->", registry)
  18.     f1()
  19.     f2()
  20.     f3()
  21. if __name__ == "__main__":
  22.     main()

從結(jié)果能看出來:

  • @register作用到f1和f2上,在導(dǎo)入時(shí),在main()調(diào)用前就執(zhí)行了。
  • f3沒有裝飾器,就沒有在main()調(diào)用前執(zhí)行@register。
  • 在main()調(diào)用后,明確調(diào)用f1()、f2()、f3()才執(zhí)行函數(shù)。

import模塊能看得更明顯:

 
 
 
 
  1. >>> import registration
  2. running register()
  3. running register()

裝飾器在導(dǎo)入時(shí)就執(zhí)行了。

使用裝飾器改進(jìn)策略模式

在《Python設(shè)計(jì)模式知多少》文章中提到了裝飾器可以更優(yōu)雅的實(shí)現(xiàn)策略模式的最佳策略,它的實(shí)現(xiàn)代碼如下:

 
 
 
 
  1. promos = []
  2. def promotion(promo_func):
  3.     promos.append(promo_func)
  4.     return promo_func
  5. @promotion
  6. def fidelity(order):
  7.     """5% discount for customers with 1000 or more fidelity points"""
  8.     return order.total() * .05 if order.customer.fidelity >= 1000 else 0
  9. @promotion
  10. def bulk_item(order):
  11.     """10% discount for each LineItem with 20 or more units"""
  12.     discount = 0
  13.     for item in order.cart:
  14.         if item.quantity >= 20:
  15.             discount += item.total() * .1
  16.     return discount
  17. @promotion
  18. def large_order(order):
  19.     """7% discount for orders with 10 or more distinct items"""
  20.     distinct_items = {item.product for item in order.cart}
  21.     if len(distinct_items) >= 10:
  22.         return order.total() * .07
  23.     return 0
  24. def best_promo(order):
  25.     """Select best discount available
  26.     """
  27.     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)格來說,它只是語法糖。要理解裝飾器,需要區(qū)別導(dǎo)入時(shí)和運(yùn)行時(shí),裝飾器在導(dǎo)入時(shí)就會(huì)執(zhí)行。最后使用裝飾器對(duì)策略模式的最佳策略進(jìn)行了優(yōu)化。為了進(jìn)一步學(xué)習(xí)函數(shù)裝飾器,得先明白另外一個(gè)很重要的概念:閉包。

參考資料:

《流暢的Python》


新聞標(biāo)題:一篇學(xué)會(huì)Python函數(shù)裝飾器基礎(chǔ)知識(shí)
瀏覽地址:http://www.5511xx.com/article/dpcjcge.html