• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

深入浅出分析Python装饰器用法

python 搞代码 4年前 (2022-01-08) 24次浏览 已收录 0个评论

这篇文章主要介绍了Python装饰器用法,结合实例形式对比分析了Python装饰器的定义与使用技巧,需要的朋友可以参考下

本文实例讲述了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 @ita def sayhi(): return 'hi' 

改进一:

 class sty(object): def __init__(self, tag): self.tag = tag def __call__(self, f): def wraper(): return "{res}".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 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(*<strong style="color:transparent">来源gao@daima#com搞(%代@#码@网</strong>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)  # 守护 t.start() 

改进:进程锁

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

更多关于Python相关内容可查看本站专题:《Python数据结构与算法教程》、《Python Socket编程技巧总结》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》及《Python入门与进阶经典教程》

希望本文所述对大家Python程序设计有所帮助。

以上就是深入浅出分析Python装饰器用法的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:深入浅出分析Python装饰器用法

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址