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

python函數(shù)裝飾器用法實例詳解

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

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

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

#! 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函數(shù)就變了,相當于是wrapper了 
def foo(a,b): 
  pass 
#不帶參數(shù)的裝飾器 
# 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 

裝飾器修飾帶參數(shù)的函數(shù):

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 

裝飾器修飾不確定參數(shù)個數(shù)的函數(shù):

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 

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

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命令行參數(shù)argparse模塊基本用法詳解

    python命令行參數(shù)argparse模塊基本用法詳解

    argparse?是python自帶的命令行參數(shù)解析包,可以用來方便地讀取命令行參數(shù),這篇文章主要介紹了python命令行參數(shù)-argparse模塊基本用法,需要的朋友可以參考下
    2023-01-01
  • 使用Python實現(xiàn)跳一跳自動跳躍功能

    使用Python實現(xiàn)跳一跳自動跳躍功能

    這篇文章主要介紹了使用Python實現(xiàn)跳一跳自動跳躍功能,本文圖文并茂通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-07-07
  • PyTorch中的squeeze()和unsqueeze()解析與應用案例

    PyTorch中的squeeze()和unsqueeze()解析與應用案例

    這篇文章主要介紹了PyTorch中的squeeze()和unsqueeze()解析與應用案例,文章內(nèi)容介紹詳細,需要的小伙伴可以參考一下,希望對你有所幫助
    2022-03-03
  • python文字轉(zhuǎn)語音的實例代碼分析

    python文字轉(zhuǎn)語音的實例代碼分析

    在本篇文章里小編給大家整理的是關于python文字轉(zhuǎn)語音的實例代碼分析,有需要的朋友們可以參考下。
    2019-11-11
  • Python3實現(xiàn)取圖片中特定的像素替換指定的顏色示例

    Python3實現(xiàn)取圖片中特定的像素替換指定的顏色示例

    這篇文章主要介紹了Python3實現(xiàn)取圖片中特定的像素替換指定的顏色,涉及Python3針對圖片文件的讀取、轉(zhuǎn)換、生成等相關操作技巧,需要的朋友可以參考下
    2019-01-01
  • python中DataFrame常用的描述性統(tǒng)計分析方法詳解

    python中DataFrame常用的描述性統(tǒng)計分析方法詳解

    這篇文章主要介紹了python中DataFrame常用的描述性統(tǒng)計分析方法詳解,描述性統(tǒng)計分析是通過圖表或數(shù)學方法,對數(shù)據(jù)資料進行整理、分析,并對數(shù)據(jù)的分布狀態(tài)、數(shù)字特征和隨機變量之間的關系進行估計和描述的方法,需要的朋友可以參考下
    2023-07-07
  • Django中如何用xlwt生成表格的方法步驟

    Django中如何用xlwt生成表格的方法步驟

    這篇文章主要介紹了Django中如何用xlwt生成表格的方法步驟,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-01-01
  • python 解決selenium 中的 .clear()方法失效問題

    python 解決selenium 中的 .clear()方法失效問題

    這篇文章主要介紹了python 解決selenium 中的 .clear()方法失效問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-09-09
  • Pygame的程序開始示例代碼

    Pygame的程序開始示例代碼

    這篇文章主要介紹了Pygame的程序開始的示例代碼,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-05-05
  • python多進程實現(xiàn)數(shù)據(jù)共享的示例代碼

    python多進程實現(xiàn)數(shù)據(jù)共享的示例代碼

    本文介紹了Python中多進程實現(xiàn)數(shù)據(jù)共享的方法,包括使用multiprocessing模塊和manager模塊這兩種方法,具有一定的參考價值,感興趣的可以了解一下
    2025-01-01

最新評論

铜梁县| 延庆县| 辽源市| 静海县| 大关县| 繁昌县| 浏阳市| 尼玛县| 收藏| 灵寿县| 翼城县| 天长市| 东宁县| 溧水县| 金华市| 凤阳县| 德惠市| 柏乡县| 丹凤县| 康保县| 瑞丽市| 凯里市| 仁化县| 义马市| 静海县| 博白县| 泗阳县| 体育| 翁源县| 姜堰市| 湟中县| 册亨县| 绥滨县| 永川市| 历史| 比如县| 靖江市| 资源县| 牡丹江市| 万山特区| 耒阳市|