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

python函數裝飾器用法實例詳解

 更新時間:2015年06月04日 12:17:55   作者:MaxOmnis  
這篇文章主要介紹了python函數裝飾器用法,以實例形式較為詳細的分析了Python函數裝飾器的常見使用技巧,需要的朋友可以參考下

本文實例講述了python函數裝飾器用法。分享給大家供大家參考。具體如下:

裝飾器經常被用于有切面需求的場景,較為經典的有插入日志、性能測試、事務處理等。裝飾器是解決這類問題的絕佳設計,
有了裝飾器,我們就可以抽離出大量函數中與函數功能本身無關的雷同代碼并繼續(xù)重用。概括的講,裝飾器的作用就是為已經存在的對象添加額外的功能。

#! coding=utf-8 
import time 
def timeit(func): 
  def wrapper(a): 
    start = time.clock() 
    func(1,2) 
    end =time.clock() 
    print 'used:', end - start 
    print a 
  return wrapper 
@timeit
# foo = timeit(foo)完全等價, 
# 使用之后,foo函數就變了,相當于是wrapper了 
def foo(a,b): 
  pass 
#不帶參數的裝飾器 
# wraper 將fn進行裝飾,return wraper ,返回的wraper 就是裝飾之后的fn 
def test(func): 
  def wraper(): 
    print "test start" 
    func() 
    print "end start" 
  return wraper 
@test 
def foo(): 
  print "in foo" 
foo() 

輸出:

test start 
in foo 
end start 

裝飾器修飾帶參數的函數:

def parameter_test(func): 
  def wraper(a): 
    print "test start" 
    func(a) 
    print "end start" 
  return wraper 
@parameter_test 
def parameter_foo(a): 
  print "parameter_foo:"+a 
#parameter_foo('hello') 

輸出:

>>> 
test start 
parameter_foo:hello 
end start 

裝飾器修飾不確定參數個數的函數:

def much_test(func): 
  def wraper(*args, **kwargs): 
    print "test start" 
    func(*args, **kwargs) 
    print "end start" 
  return wraper 
@much_test 
def much1(a): 
  print a 
@much_test 
def much2(a,b,c,d ): 
  print a,b,c,d 
much1('a') 
much2(1,2,3,4) 

輸出:

test start 
a 
end start 
test start 
1 2 3 4 
end start 

帶參數的裝飾器,再包一層就可以了:

def tp(name,age): 
  def much_test(func): 
    print 'in much_test' 
    def wraper(*args, **kwargs): 
      print "test start" 
      print str(name),'at:'+str(age) 
      func(*args, **kwargs) 
      print "end start" 
    return wraper 
  return much_test 
@tp('one','10') 
def tpTest(parameter): 
  print parameter 
tpTest('python....') 

輸出:

in much_test 
test start 
one at:10 
python.... 
end start 

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.(不需要對象實例") 
def deco(cls): 
  '''cls 必須實現(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() 

輸出:

>>> 
before myfunc called [__main__.locker].
locker.acquire() called.(這是靜態(tài)方法)
 myfunc() called.
locker.release() called.(不需要對象實例
>>> 

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 必須實現(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 
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))

輸出:

before myfunc called.
mylocker.acquire() called.
 myfunc() called.
 mylocker.unlock() called.
before myfunc called.
mylocker.acquire() called.
 myfunc() called.
 mylocker.unlock() called.
None
before __deco called.
mylocker.acquire() called.
before myfunc2 called.
lockerex.acquire() called.
 myfunc2() called.
 lockerex.unlock() called.
 mylocker.unlock() called.
3
before __deco called.
mylocker.acquire() called.
before myfunc2 called.
lockerex.acquire() called.
 myfunc2() called.
 lockerex.unlock() called.
 mylocker.unlock() called.
7

希望本文所述對大家的Python程序設計有所幫助。

相關文章

  • Python OpenCV實戰(zhàn)之與機器學習的碰撞

    Python OpenCV實戰(zhàn)之與機器學習的碰撞

    機器學習是人工智能的子集,為計算機以及其它具有計算能力的系統(tǒng)提供自動預測或決策的能力。本文主要介紹了OpenCV 提供的常見機器學習算法和技術,用于解決計算機視覺項目中的實際問題,需要的朋友可以參考一下
    2021-12-12
  • PyTorch之怎樣選擇合適的優(yōu)化器和損失函數

    PyTorch之怎樣選擇合適的優(yōu)化器和損失函數

    這篇文章主要介紹了PyTorch怎樣選擇合適的優(yōu)化器和損失函數問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-02-02
  • Python基礎之賦值,淺拷貝,深拷貝的區(qū)別

    Python基礎之賦值,淺拷貝,深拷貝的區(qū)別

    這篇文章主要介紹了Python基礎之賦值,淺拷貝,深拷貝的區(qū)別,文中有非常詳細的代碼示例,對正在學習python基礎的小伙伴們也有非常好的幫助,需要的朋友可以參考下
    2021-04-04
  • Python新手如何進行閉包時綁定變量操作

    Python新手如何進行閉包時綁定變量操作

    在本篇文章里小編給大家分享的是關于Python新閉包時綁定變量實例,有興趣的朋友們可以參考下。
    2020-05-05
  • python實現(xiàn)簡單socket通信的方法

    python實現(xiàn)簡單socket通信的方法

    這篇文章主要介紹了python實現(xiàn)簡單socket通信的方法,結合實例形式分析了socket通信服務端與客戶端的具體實現(xiàn)技巧,需要的朋友可以參考下
    2016-04-04
  • python分布式系統(tǒng)Celery安裝使用實例講解

    python分布式系統(tǒng)Celery安裝使用實例講解

    這篇文章主要為大家介紹了python分布式系統(tǒng)Celery安裝使用實例講解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-12-12
  • Django進階深入理解使用類視圖和中間件示例

    Django進階深入理解使用類視圖和中間件示例

    這篇文章主要為大家介紹了Django高級指南之深入理解和使用類視圖和中間件示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-08-08
  • python+matplotlib繪制3D條形圖實例代碼

    python+matplotlib繪制3D條形圖實例代碼

    這篇文章主要介紹了python+matplotlib繪制3D條形圖實例代碼,具有一定借鑒價值,需要的朋友可以參考下
    2018-01-01
  • Python入門篇之正則表達式

    Python入門篇之正則表達式

    正則表達式是一個很有用的工具,可處理復雜的字符匹配和替換工作。在Python中內置了一個re模塊以支持正則表達式。本文我們就來詳細探討下Python中正則表達式。
    2014-10-10
  • python通過exifread模塊獲得圖片exif信息的方法

    python通過exifread模塊獲得圖片exif信息的方法

    這篇文章主要介紹了python通過exifread模塊獲得圖片exif信息的方法,實例分析了Python操作exifread模塊的技巧,需要的朋友可以參考下
    2015-03-03

最新評論