内射老阿姨1区2区3区4区_久久精品人人做人人爽电影蜜月_久久国产精品亚洲77777_99精品又大又爽又粗少妇毛片

python裝飾器如何限制函數(shù)調(diào)用次數(shù)-創(chuàng)新互聯(lián)

小編給大家分享一下python裝飾器如何限制函數(shù)調(diào)用次數(shù),相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

創(chuàng)新互聯(lián)服務項目包括平泉網(wǎng)站建設(shè)、平泉網(wǎng)站制作、平泉網(wǎng)頁制作以及平泉網(wǎng)絡(luò)營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢、行業(yè)經(jīng)驗、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,平泉網(wǎng)站推廣取得了明顯的社會效益與經(jīng)濟效益。目前,我們服務的客戶以成都為中心已經(jīng)輻射到平泉省份的部分城市,未來相信會繼續(xù)擴大服務區(qū)域并繼續(xù)獲得客戶的支持與信任!

裝飾器分為帶參數(shù)得裝飾器以及不帶參數(shù)得裝飾器。

#不帶參數(shù)的裝飾器
@dec1
@dec2
def func():
  ...
#這個函數(shù)聲明等價于
func = dec1(dec2(func))
#帶參數(shù)的裝飾器
@dec(some_args)
def func():
  ...
#這個函數(shù)聲明等價于
func = dec(some_args)(func)

不帶參數(shù)的裝飾器需要注意的一些細節(jié)

1. 關(guān)于裝飾器函數(shù)(decorator)本身

因此一個裝飾器一般對應兩個函數(shù),一個是decorator函數(shù),用來進行一些初始化操作處理,一個是decorated_func用來實現(xiàn)對被裝飾的函數(shù)func的額外處理。并且為了保持對func的引用,decorated_func一般作為decorator的內(nèi)部函數(shù)

def decorator(func):
  def decorator_func()
    func()
  return decorated_func

decorator函數(shù)只在函數(shù)聲明的時候被調(diào)用一次

裝飾器實際上是語法糖,在聲明函數(shù)之后就會被調(diào)用,產(chǎn)生decorated_func,并把func符號的引用替換為decorated_func。之后每次調(diào)用func函數(shù),實際調(diào)用的是decorated_func(這個很重要,裝飾之后,其實每次調(diào)用的是decorated_func)。

>>> def decorator(func):
...   def decorated_func():
...     func(1)
...   return decorated_func
... 
#聲明時就被調(diào)用
>>> @decorator
... def func(x):
...   print x
... 
decorator being called 
#使用func()函數(shù)實際上使用的是decorated_func函數(shù)
>>> func()
1
>>> func.__name__
'decorated_func'

如果要保證返回的decorated_func的函數(shù)名與func的函數(shù)名相同,應當在decorator函數(shù)返回decorated_func之前,加入decorated_func.name = func.name, 另外functools模塊提供了wraps裝飾器,可以完成這一動作。

#@wraps(func)的操作相當于
#在return decorated_func之前,執(zhí)行
#decorated_func.__name__ = func.__name__
#func作為裝飾器參數(shù)傳入, 
#decorated_func則作為wraps返回的函數(shù)的參數(shù)傳入
>>> def decorator(func):
...   @wraps(func)
...   def decorated_func():
...     func(1)
...   return decorated_func
... 
#聲明時就被調(diào)用
>>> @decorator
... def func(x):
...   print x
... 
decorator being called 
#使用func()函數(shù)實際上使用的是decorated_func函數(shù)
>>> func()
1
>>> func.__name__
'func'

decorator函數(shù)局部變量的妙用

因為closure的特性(詳見(1)部分閉包部分的詳解),decorator聲明的變量會被decorated_func.func_closure引用,所以調(diào)用了decorator方法結(jié)束之后,decorator方法的局部變量也不會被回收,因此可以用decorator方法的局部變量作為計數(shù)器,緩存等等。

值得注意的是,如果要改變變量的值,該變量一定要是可變對象,因此就算是計數(shù)器,也應當用列表來實現(xiàn)。并且聲明一次函數(shù)調(diào)用一次decorator函數(shù),所以不同函數(shù)的計數(shù)器之間互不沖突,例如:

#!/usr/bin/env python
#filename decorator.py
def decorator(func):
  #注意這里使用可變對象
  a = [0]
  def decorated_func(*args,**keyargs):
    func(*args, **keyargs)
    #因為閉包是淺拷貝,如果是不可變對象,每次調(diào)用完成后符號都會被清空,導致錯誤
    a[0] += 1
    print "%s have bing called %d times" % (func.__name__, a[0])
  return decorated_func
@decorator
def func(x):
  print x
@decorator
def theOtherFunc(x):
  print x

下面我們開始寫代碼:

#coding=UTF-8
#!/usr/bin/env python
#filename decorator.py
import time
from functools import wraps
def decorator(func):
  "cache for function result, which is immutable with fixed arguments"
  print "initial cache for %s" % func.__name__
  cache = {}
  @wraps(func)
  def decorated_func(*args,**kwargs):
    # 函數(shù)的名稱作為key
    key = func.__name__
    result = None
    #判斷是否存在緩存
    if key in cache.keys():
      (result, updateTime) = cache[key]
      #過期時間固定為10秒
      if time.time() -updateTime < 10:
        print "limit call 10s", key
        result = updateTime
      else :
        print "cache expired !!! can call "
        result = None
    else:
      print "no cache for ", key
    #如果過期,或則沒有緩存調(diào)用方法
    if result is None:
      result = func(*args, **kwargs)
      cache[key] = (result, time.time())
    return result
  return decorated_func
@decorator
def func(x):
  print 'call func'

隨便測試了下,基本沒有問題。

>>> from decorator import func
initial cache for func
>>> func(1)
no cache for func
call func
>>> func(1)
limit call 10s func
1488082913.239092
>>> func(1)
cache expired !!! can call
call func
>>> func(1)
limit call 10s func
1488082923.298204
>>> func(1)
cache expired !!! can call
call func
>>> func(1)
limit call 10s func
1488082935.165979
>>> func(1)
limit call 10s func
1488082935.165979

以上是“python裝飾器如何限制函數(shù)調(diào)用次數(shù)”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學習更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)成都網(wǎng)站設(shè)計公司行業(yè)資訊頻道!

另外有需要云服務器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。

分享題目:python裝飾器如何限制函數(shù)調(diào)用次數(shù)-創(chuàng)新互聯(lián)
分享地址:http://www.rwnh.cn/article10/ceiigo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App設(shè)計、網(wǎng)站制作、商城網(wǎng)站、外貿(mào)網(wǎng)站建設(shè)、Google響應式網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站建設(shè)網(wǎng)站維護公司
淳化县| 临猗县| 土默特左旗| 黄骅市| 卢湾区| 漯河市| 上思县| 津南区| 双鸭山市| 醴陵市| 翁牛特旗| 屏东市| 海安县| 万载县| 虎林市| 永泰县| 贵港市| 洪泽县| 海晏县| 冀州市| 舞钢市| 丹江口市| 雷山县| 嘉善县| 正镶白旗| 龙川县| 花莲县| 柳州市| 谷城县| 改则县| 赤峰市| 苏尼特右旗| 大港区| 济阳县| 临江市| 闵行区| 双柏县| 南部县| 神池县| 石城县| 晋江市|