欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

九步學(xué)會(huì)Python裝飾器

 更新時(shí)間:2015年05月09日 11:53:59   作者:守株待兔  
這篇文章主要介紹了Python裝飾器的用法,以實(shí)例形式較為詳細(xì)的介紹了Python裝飾器的使用方法,需要的朋友可以參考下

本文實(shí)例講述了Python裝飾器。分享給大家供大家參考。具體分析如下:

這是在Python學(xué)習(xí)小組上介紹的內(nèi)容,現(xiàn)學(xué)現(xiàn)賣、多練習(xí)是好的學(xué)習(xí)方式。
第一步:最簡(jiǎn)單的函數(shù),準(zhǔn)備附加額外功能

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

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

# -*- coding:gbk -*-
'''示例2: 替換函數(shù)(裝飾)
裝飾函數(shù)的參數(shù)是被裝飾的函數(shù)對(duì)象,返回原函數(shù)對(duì)象
裝飾的實(shí)質(zhì)語(yǔ)句: 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()

第三步:使用語(yǔ)法糖@來(lái)裝飾函數(shù)

# -*- coding:gbk -*-
'''示例3: 使用語(yǔ)法糖@來(lái)裝飾函數(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ù)來(lái)確保每次新函數(shù)都被調(diào)用

# -*- coding:gbk -*-
'''示例4: 使用內(nèi)嵌包裝函數(shù)來(lái)確保每次新函數(shù)都被調(diào)用,
內(nèi)嵌包裝函數(shù)的形參和返回值與原函數(shù)相同,
裝飾函數(shù)返回內(nèi)嵌包裝函數(shù)對(duì)象'''
 
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()

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

# -*- coding:gbk -*-
'''示例5: 對(duì)帶參數(shù)的函數(shù)進(jìn)行裝飾,
內(nèi)嵌包裝函數(shù)的形參和返回值與原函數(shù)相同,
裝飾函數(shù)返回內(nèi)嵌包裝函數(shù)對(duì)象'''
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)

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

# -*- coding:gbk -*-
'''示例6: 對(duì)參數(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.(不需要對(duì)象實(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í)演示了對(duì)一個(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í)演示了對(duì)一個(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))

希望本文所述對(duì)大家的Python程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • pandas去除重復(fù)列的實(shí)現(xiàn)方法

    pandas去除重復(fù)列的實(shí)現(xiàn)方法

    這篇文章主要介紹了pandas去除重復(fù)列的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-01-01
  • 詳解Python中四種關(guān)系圖數(shù)據(jù)可視化的效果對(duì)比

    詳解Python中四種關(guān)系圖數(shù)據(jù)可視化的效果對(duì)比

    python關(guān)系圖的可視化主要就是用來(lái)分析一堆數(shù)據(jù)中,每一條數(shù)據(jù)的節(jié)點(diǎn)之間的連接關(guān)系從而更好的分析出人物或其他場(chǎng)景中存在的關(guān)聯(lián)關(guān)系。本文將制作四個(gè)不同的關(guān)系圖的可視化效果,感興趣的可以了解一下
    2022-11-11
  • Python目錄下文件讀取方式

    Python目錄下文件讀取方式

    這篇文章主要介紹了Python目錄下文件讀取方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • python編程培訓(xùn) python培訓(xùn)靠譜嗎

    python編程培訓(xùn) python培訓(xùn)靠譜嗎

    現(xiàn)在大家都知道,比較火的編程語(yǔ)言就是python了,很多朋友都想學(xué)習(xí)python編程,想上一個(gè)好的python培訓(xùn)班,小編今天給大家全面分析一下關(guān)于python編程培訓(xùn)方面的問(wèn)題,希望能給你答疑解惑。
    2018-01-01
  • Python?的Json?模塊編碼詳解

    Python?的Json?模塊編碼詳解

    這篇文章主要為大家介紹了Python?的Json?模塊編碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助<BR>
    2021-11-11
  • 詳解Python裝飾器之@property

    詳解Python裝飾器之@property

    今天帶大家學(xué)習(xí)python裝飾器的相關(guān)知識(shí),文中對(duì)Python @property做了非常詳細(xì)的介紹,對(duì)正在學(xué)習(xí)python的小伙伴們很有幫助,需要的朋友可以參考下
    2021-05-05
  • python讀取文件列表并排序的實(shí)現(xiàn)示例

    python讀取文件列表并排序的實(shí)現(xiàn)示例

    本文主要介紹了python讀取文件列表并排序的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07
  • pytorch中的model.eval()和BN層的使用

    pytorch中的model.eval()和BN層的使用

    這篇文章主要介紹了pytorch中的model.eval()和BN層的使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-05-05
  • python 自動(dòng)化辦公之批量修改文件名實(shí)操

    python 自動(dòng)化辦公之批量修改文件名實(shí)操

    這篇文章主要介紹了python 自動(dòng)化辦公之批量修改文件名實(shí)操,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-07-07
  • python中的list 查找與過(guò)濾方法整合

    python中的list 查找與過(guò)濾方法整合

    這篇文章主要介紹了python中的list 查找與過(guò)濾方法整合,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-01-01

最新評(píng)論