最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python裝飾器入門學(xué)習(xí)教程(九步學(xué)習(xí))

 更新時(shí)間:2016年01月28日 10:33:41   投稿:mrr  
裝飾器(decorator)是一種高級Python語法。裝飾器可以對一個(gè)函數(shù)、方法或者類進(jìn)行加工。本文給大家介紹Python裝飾器入門學(xué)習(xí)教程(九步學(xué)習(xí)),對python裝飾器相關(guān)知識感興趣的朋友一起學(xué)習(xí)吧

裝飾器(decorator)是一種高級Python語法。裝飾器可以對一個(gè)函數(shù)、方法或者類進(jìn)行加工。在Python中,我們有多種方法對函數(shù)和類進(jìn)行加工,比如在Python閉包中,我們見到函數(shù)對象作為某一個(gè)函數(shù)的返回結(jié)果。相對于其它方式,裝飾器語法簡單,代碼可讀性高。因此,裝飾器在Python項(xiàng)目中有廣泛的應(yīng)用。

這是在Python學(xué)習(xí)小組上介紹的內(nèi)容,現(xiàn)學(xué)現(xiàn)賣、多練習(xí)是好的學(xué)習(xí)方式。

第一步:最簡單的函數(shù),準(zhǔn)備附加額外功能

# -*- coding:gbk -*-
'''示例1: 最簡單的函數(shù),表示調(diào)用了兩次'''
def myfunc():
print("myfunc() called.")
myfunc()
myfunc() 

第二步:使用裝飾函數(shù)在函數(shù)執(zhí)行前和執(zhí)行后分別附加額外功能

# -*- coding:gbk -*-
'''示例2: 替換函數(shù)(裝飾)
裝飾函數(shù)的參數(shù)是被裝飾的函數(shù)對象,返回原函數(shù)對象
裝飾的實(shí)質(zhì)語句: myfunc = deco(myfunc)'''
def deco(func):
print("before myfunc() called.")
func()
print(" after myfunc() called.")
return func
def myfunc():
print(" myfunc() called.")
myfunc = deco(myfunc)
myfunc()
myfunc() 

第三步:使用語法糖@來裝飾函數(shù)

# -*- coding:gbk -*-
'''示例3: 使用語法糖@來裝飾函數(shù),相當(dāng)于“myfunc = deco(myfunc)”
但發(fā)現(xiàn)新函數(shù)只在第一次被調(diào)用,且原函數(shù)多調(diào)用了一次'''
def deco(func):
print("before myfunc() called.")
func()
print(" after myfunc() called.")
return func
@deco
def myfunc():
print(" myfunc() called.")
myfunc()
myfunc() 

第四步:使用內(nèi)嵌包裝函數(shù)來確保每次新函數(shù)都被調(diào)用

# -*- coding:gbk -*-
'''示例4: 使用內(nèi)嵌包裝函數(shù)來確保每次新函數(shù)都被調(diào)用,
內(nèi)嵌包裝函數(shù)的形參和返回值與原函數(shù)相同,裝飾函數(shù)返回內(nèi)嵌包裝函數(shù)對象'''
def deco(func):
def _deco():
print("before myfunc() called.")
func()
print(" after myfunc() called.")
# 不需要返回func,實(shí)際上應(yīng)返回原函數(shù)的返回值
return _deco
@deco
def myfunc():
print(" myfunc() called.")
return 'ok'
myfunc()
myfunc() 

第五步:對帶參數(shù)的函數(shù)進(jìn)行裝飾

# -*- coding:gbk -*-
'''示例5: 對帶參數(shù)的函數(shù)進(jìn)行裝飾,
內(nèi)嵌包裝函數(shù)的形參和返回值與原函數(shù)相同,裝飾函數(shù)返回內(nèi)嵌包裝函數(shù)對象'''
def deco(func):
def _deco(a, b):
print("before myfunc() called.")
ret = func(a, b)
print(" after myfunc() called. result: %s" % ret)
return ret
return _deco
@deco
def myfunc(a, b):
print(" myfunc(%s,%s) called." % (a, b))
return a + b
myfunc(1, 2)
myfunc(3, 4) 

第六步:對參數(shù)量不確定的函數(shù)進(jìn)行裝飾

# -*- coding:gbk -*-
'''示例6: 對參數(shù)數(shù)量不確定的函數(shù)進(jìn)行裝飾,
參數(shù)用(*args, **kwargs),自動(dòng)適應(yīng)變參和命名參數(shù)'''
def deco(func):
def _deco(*args, **kwargs):
print("before %s called." % func.__name__)
ret = func(*args, **kwargs)
print(" after %s called. result: %s" % (func.__name__, ret))
return ret
return _deco
@deco
def myfunc(a, b):
print(" myfunc(%s,%s) called." % (a, b))
return a+b
@deco
def myfunc2(a, b, c):
print(" myfunc2(%s,%s,%s) called." % (a, b, c))
return a+b+c
myfunc(1, 2)
myfunc(3, 4)
myfunc2(1, 2, 3)
myfunc2(3, 4, 5) 

第七步:讓裝飾器帶參數(shù)

# -*- coding:gbk -*-
'''示例7: 在示例4的基礎(chǔ)上,讓裝飾器帶參數(shù),
和上一示例相比在外層多了一層包裝。
裝飾函數(shù)名實(shí)際上應(yīng)更有意義些'''
def deco(arg):
def _deco(func):
def __deco():
print("before %s called [%s]." % (func.__name__, arg))
func()
print(" after %s called [%s]." % (func.__name__, arg))
return __deco
return _deco
@deco("mymodule")
def myfunc():
print(" myfunc() called.")
@deco("module2")
def myfunc2():
print(" myfunc2() called.")
myfunc()
myfunc2() 

第八步:讓裝飾器帶 類 參數(shù)

# -*- coding:gbk -*-
'''示例8: 裝飾器帶類參數(shù)'''
class locker:
def __init__(self):
print("locker.__init__() should be not called.")
@staticmethod
def acquire():
print("locker.acquire() called.(這是靜態(tài)方法)")
@staticmethod
def release():
print(" locker.release() called.(不需要對象實(shí)例)")
def deco(cls):
'''cls 必須實(shí)現(xiàn)acquire和release靜態(tài)方法'''
def _deco(func):
def __deco():
print("before %s called [%s]." % (func.__name__, cls))
cls.acquire()
try:
return func()
finally:
cls.release()
return __deco
return _deco
@deco(locker)
def myfunc():
print(" myfunc() called.")
myfunc()
myfunc() 

第九步:裝飾器帶類參數(shù),并分拆公共類到其他py文件中,同時(shí)演示了對一個(gè)函數(shù)應(yīng)用多個(gè)裝飾器

# -*- coding:gbk -*-
'''mylocker.py: 公共類 for 示例9.py'''
class mylocker:
def __init__(self):
print("mylocker.__init__() called.")
@staticmethod
def acquire():
print("mylocker.acquire() called.")
@staticmethod
def unlock():
print(" mylocker.unlock() called.")
class lockerex(mylocker):
@staticmethod
def acquire():
print("lockerex.acquire() called.")
@staticmethod
def unlock():
print(" lockerex.unlock() called.")
def lockhelper(cls):
'''cls 必須實(shí)現(xiàn)acquire和release靜態(tài)方法'''
def _deco(func):
def __deco(*args, **kwargs):
print("before %s called." % func.__name__)
cls.acquire()
try:
return func(*args, **kwargs)
finally:
cls.unlock()
return __deco
return _deco 
# -*- coding:gbk -*-

'''示例9: 裝飾器帶類參數(shù),并分拆公共類到其他py文件中

同時(shí)演示了對一個(gè)函數(shù)應(yīng)用多個(gè)裝飾器'''

from mylocker import *
class example:
@lockhelper(mylocker)
def myfunc(self):
print(" myfunc() called.")
@lockhelper(mylocker)
@lockhelper(lockerex)
def myfunc2(self, a, b):
print(" myfunc2() called.")
return a + b
if __name__=="__main__":
a = example()
a.myfunc()
print(a.myfunc())
print(a.myfunc2(1, 2))
print(a.myfunc2(3, 4)) 

以上給大家分享了Python裝飾器入門學(xué)習(xí)教程(九步學(xué)習(xí)),希望對大家有所幫助。

相關(guān)文章

  • Python基礎(chǔ)類繼承重寫實(shí)現(xiàn)原理解析

    Python基礎(chǔ)類繼承重寫實(shí)現(xiàn)原理解析

    這篇文章主要介紹了Python基礎(chǔ)類繼承重寫實(shí)現(xiàn)原理解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-04-04
  • 淺談Python中eval的強(qiáng)大與危害

    淺談Python中eval的強(qiáng)大與危害

    這篇文章主要介紹了Python中eval的強(qiáng)大與危害,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • pycharm 如何取消連按兩下shift出現(xiàn)的全局搜索

    pycharm 如何取消連按兩下shift出現(xiàn)的全局搜索

    這篇文章主要介紹了pycharm 如何取消連按兩下shift出現(xiàn)的全局搜索?下面小編就為大家介紹一下解決方法,還等什么?一起跟隨小編過來看看吧
    2021-01-01
  • 解決Django layui {{}}沖突的問題

    解決Django layui {{}}沖突的問題

    今天小編就為大家分享一篇解決Django layui {{}}沖突的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-08-08
  • Python處理application/json錯(cuò)誤的方法詳解

    Python處理application/json錯(cuò)誤的方法詳解

    這篇文章主要為大家詳細(xì)介紹了python使用httpx_sse調(diào)用sse流式接口對響應(yīng)格式為application/json的錯(cuò)誤信息處理的相關(guān)知識,需要的可以了解下
    2025-02-02
  • pygame+opencv實(shí)現(xiàn)讀取視頻幀的方法示例

    pygame+opencv實(shí)現(xiàn)讀取視頻幀的方法示例

    由于pygame.movie.Movie.play()只支持MPEG格式的視頻,所以決定使用與opencv讀取視頻幀的畫面,本文就詳細(xì)的介紹了pygame+opencv實(shí)現(xiàn)讀取視頻幀,感興趣的可以了解一下
    2021-12-12
  • django2.2安裝錯(cuò)誤最全的解決方案(小結(jié))

    django2.2安裝錯(cuò)誤最全的解決方案(小結(jié))

    這篇文章主要介紹了django2.2安裝錯(cuò)誤最全的解決方案(小結(jié)),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • 用60行代碼實(shí)現(xiàn)Python自動(dòng)搶微信紅包

    用60行代碼實(shí)現(xiàn)Python自動(dòng)搶微信紅包

    這篇文章主要介紹了用60行代碼實(shí)現(xiàn)Python自動(dòng)搶微信紅包,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-02-02
  • Python實(shí)現(xiàn)一鍵下載視頻腳本

    Python實(shí)現(xiàn)一鍵下載視頻腳本

    這篇文章主要為大家分享了一個(gè)用Python編寫的一鍵下載視頻的腳本,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2022-01-01
  • python讀寫修改Excel之xlrd&xlwt&xlutils

    python讀寫修改Excel之xlrd&xlwt&xlutils

    這篇文章主要介紹了python讀寫修改Excel之xlrd&xlwt&xlutils,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03

最新評論

吉安市| 手游| 东宁县| 甘孜县| 武乡县| 图片| 札达县| 深州市| 黄陵县| 库伦旗| 麻栗坡县| 墨竹工卡县| 龙井市| 资溪县| 吴旗县| 安溪县| 咸阳市| 扬中市| 石泉县| 巴林左旗| 门头沟区| 卢氏县| 甘肃省| 清水河县| 崇阳县| 安陆市| 安顺市| 卢湾区| 柘荣县| 九龙坡区| 楚雄市| 三原县| 军事| 延吉市| 合肥市| 周至县| 剑河县| 迁西县| 杭锦旗| 景宁| 宜兰县|