亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

深入淺出分析Python裝飾器用法

 更新時(shí)間:2017年07月28日 10:12:31   作者:羅兵  
這篇文章主要介紹了Python裝飾器用法,結(jié)合實(shí)例形式對(duì)比分析了Python裝飾器的定義與使用技巧,需要的朋友可以參考下

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

用類(lèi)作為裝飾器

示例一

最初代碼:

class bol(object):
 def __init__(self, func):
  self.func = func
 def __call__(self):
  return "<b>{}</b>".format(self.func())
class ita(object):
 def __init__(self, func):
  self.func = func
 def __call__(self):
  return "<i>{}</i>".format(self.func())
@bol
@ita
def sayhi():
 return 'hi'

改進(jìn)一:

class sty(object):
 def __init__(self, tag):
  self.tag = tag
 def __call__(self, f):
  def wraper():
   return "<{tag}>{res}</{tag}>".format(res=f(), tag=self.tag)
  return wraper
@sty('b')
@sty('i')
def sayhi():
 return 'hi'

改進(jìn)二:

class sty(object):
 def __init__(self, *tags):
  self.tags = tags
 def __call__(self, f):
  def wraper():
   n = len(self.tags)
   return "{0}{1}{2}".format(('<{}>'*n).format(*self.tags), f(), ('</{}>'*n).format(*reversed(self.tags)))
  return wraper
@sty('b', 'i')
def sayhi():
 return 'hi'
print(sayhi())

改進(jìn)三:

class sty(object):
 def __init__(self, *tags):
  self.tags = tags
 def __call__(self, f):
  def wraper(*args, **kwargs):
   n = len(self.tags)
   return "{0}{1}{2}".format(('<{}>'*n).format(*self.tags), f(*args, **kwargs), ('</{}>'*n).format(*reversed(self.tags)))
  return wraper
@sty('b', 'i')
def say(word='Hi'):
 return word
print(say())
print(say('Hello'))

示例二

最初代碼:

import threading
import time
class DecoratorClass(object):
  def __init__(self):
    self.thread = None
  def __call__(self, func, *args, **kwargs):
    def wrapped_func(*args, **kwargs):
      curr_thread = threading.currentThread().getName()
      self.thread = curr_thread
      print('\nthread name before running func:', self.thread)
      ret_val = func()
      print('\nthread name after running func:', self.thread)
      return ret_val
    return wrapped_func
@DecoratorClass()
def decorated_with_class():
  print('running decorated w class')
  time.sleep(1)
  return
threads = []
for i in range(5):
  t = threading.Thread(target=decorated_with_class)
  threads.append(t)
  t.setDaemon(True)  # 守護(hù)
  t.start()

改進(jìn):進(jìn)程鎖

import threading
import time
class DecoratorClass(object):
  def __init__(self):
    self.thread = None
    self.lock = threading.Lock()
  def __call__(self, func, *args, **kwargs):
    def wrapped_func(*args, **kwargs):
      self.lock.acquire()
      curr_thread = threading.currentThread().getName()
      self.thread = curr_thread
      print('thread name before running func:', self.thread)
      ret_val = func()
      print('\nthread name after running func:', self.thread)
      self.lock.release()
      return ret_val
    return wrapped_func
@DecoratorClass()
def decorated_with_class():
  print('Let me sleep 1 second...')
  time.sleep(1)
  return
threads = []
for i in range(5):
  t = threading.Thread(target=decorated_with_class)
  threads.append(t)
  t.start()

更多關(guān)于Python相關(guān)內(nèi)容可查看本站專(zhuān)題:《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python Socket編程技巧總結(jié)》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》及《Python入門(mén)與進(jìn)階經(jīng)典教程

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

相關(guān)文章

  • Python抓取框架 Scrapy的架構(gòu)

    Python抓取框架 Scrapy的架構(gòu)

    這篇文章主要為大家詳細(xì)介紹了Python抓取框架,針對(duì)Scrapy的架構(gòu)進(jìn)行分析,感興趣的小伙伴們可以參考一下
    2016-08-08
  • Python+Matplotlib繪制雙y軸圖像的示例代碼

    Python+Matplotlib繪制雙y軸圖像的示例代碼

    這篇文章主要介紹了如何利用python的matplotlib繪制雙Y軸圖像,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)python的小伙伴們有很好地幫助,需要的朋友可以參考下
    2022-04-04
  • seaborn繪制雙變量聯(lián)合分布圖示例詳解

    seaborn繪制雙變量聯(lián)合分布圖示例詳解

    這篇文章主要為大家介紹了seaborn繪制雙變量聯(lián)合分布圖示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-12-12
  • 解決python 找不到module的問(wèn)題

    解決python 找不到module的問(wèn)題

    今天小編就為大家分享一篇解決python 找不到module的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-02-02
  • 小小聊天室Python代碼實(shí)現(xiàn)

    小小聊天室Python代碼實(shí)現(xiàn)

    這篇文章主要為大家詳細(xì)介紹了小小聊天室Python具體的實(shí)現(xiàn)代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-08-08
  • Python sys模塊中maxsize()方法教程示例

    Python sys模塊中maxsize()方法教程示例

    這篇文章主要為大家介紹了Python sys模塊中maxsize()方法教程示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-09-09
  • 在Python 3中實(shí)現(xiàn)類(lèi)型檢查器的簡(jiǎn)單方法

    在Python 3中實(shí)現(xiàn)類(lèi)型檢查器的簡(jiǎn)單方法

    這篇文章主要介紹了在Python 3中實(shí)現(xiàn)類(lèi)型檢查器的簡(jiǎn)單方法,包括對(duì)函數(shù)注解這個(gè)新特性的介紹,需要的朋友可以參考下
    2015-07-07
  • python游戲地圖最短路徑求解

    python游戲地圖最短路徑求解

    這篇文章主要為大家詳細(xì)介紹了python游戲地圖最短路徑的求解,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-01-01
  • Django中的Model操作表的實(shí)現(xiàn)

    Django中的Model操作表的實(shí)現(xiàn)

    這篇文章主要介紹了Django中的Model操作表的實(shí)現(xiàn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-07-07
  • Python wxPython庫(kù)Core組件BoxSizer用法示例

    Python wxPython庫(kù)Core組件BoxSizer用法示例

    這篇文章主要介紹了Python wxPython庫(kù)Core組件BoxSizer用法,結(jié)合實(shí)例形式分析了wxPython BoxSizer布局管理相關(guān)使用方法及操作注意事項(xiàng),需要的朋友可以參考下
    2018-09-09

最新評(píng)論