国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

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

2019-11-25 15:58:56
字體:
來源:轉載
供稿:網友

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

用類作為裝飾器

示例一

最初代碼:

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@itadef sayhi(): return 'hi'

改進一:

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'

改進二:

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())

改進三:

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 wordprint(say())print(say('Hello'))

示例二

最初代碼:

import threadingimport timeclass 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)  returnthreads = []for i in range(5):  t = threading.Thread(target=decorated_with_class)  threads.append(t)  t.setDaemon(True)  # 守護  t.start()

改進:進程鎖

import threadingimport timeclass 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)  returnthreads = []for i in range(5):  t = threading.Thread(target=decorated_with_class)  threads.append(t)  t.start()

更多關于Python相關內容可查看本站專題:《Python數據結構與算法教程》、《Python Socket編程技巧總結》、《Python函數使用技巧總結》、《Python字符串操作技巧匯總》及《Python入門與進階經典教程

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 五指山市| 和平县| 文安县| 清水县| 广丰县| 赤峰市| 乌拉特后旗| 志丹县| 焉耆| 太白县| 开化县| 乐平市| 马公市| 壶关县| 方山县| 彩票| 乌兰察布市| 张家港市| 新竹市| 栖霞市| 普兰店市| 开化县| 宜兴市| 彝良县| 平谷区| 正镶白旗| 和硕县| 北宁市| 高青县| 来安县| 齐齐哈尔市| 惠安县| 凤城市| 仁怀市| 田东县| 临沂市| 进贤县| 新巴尔虎左旗| 收藏| 叶城县| 叶城县|