老男孩python学习自修第十七天【装饰器】
装饰器:在某个方法执行前后去执行其他新定义的行为
例如:
#!/usr/bin/env python
# _*_ coding:UTF-8 _*_
def before_say_hello():
print "before hello"
def after_say_hello():
print "after hello"
def say_hello_wrapper(func):
def wrapper():
print "Before"
before_say_hello()
func()
print "After"
after_say_hello()
return wrapper
@say_hello_wrapper
def say_hello():
print "Hello!"
if __name__ == "__main__":
say_hello()
执行结果:
/Users/liudaoqiang/PycharmProjects/numpy/venv/bin/python /Users/liudaoqiang/Project/python_project/day14/decorator_test.py Before before hello Hello! After after hello Process finished with exit code 0
2.装饰器的执行步骤
(1)首先,装饰器本身也是一个函数,即say_hello_wrapper也是一个函数,也需要在执行前加入到内存
(2)当执行到@say_hello_wrapper时,即将say_hello()替换为say_hello_wrapper()里面的wrapper(), 即为相同的内存地址
(3)当执行到say_hello()时,则会执行say_hello_wrapper()
注意:使用debug模式可知装饰器的执行步骤
3. 带参数函数的装饰器
#!/usr/bin/env python
# _*_ coding:UTF-8 _*_
def before_say_hello():
print "before hello"
def after_say_hello():
print "after hello"
def say_hello_wrapper(func):
def wrapper(content):
print "Before"
before_say_hello()
func(content)
print "After"
after_say_hello()
return wrapper
@say_hello_wrapper
def say_hello(content):
print "Hello, {0}".format(content)
if __name__ == "__main__":
say_hello("World")
结果:
/Users/liudaoqiang/PycharmProjects/numpy/venv/bin/python /Users/liudaoqiang/Project/python_project/day14/decorator_test.py Before before hello Hello, World After after hello Process finished with exit code 0
注意:只需将参数加入到wrapper()中即可,因为wrapper()为替换函数
4.带返回值函数的装饰器
#!/usr/bin/env python
# _*_ coding:UTF-8 _*_
def before_say_hello():
print "before hello"
def after_say_hello():
print "after hello"
def say_hello_wrapper(func):
def wrapper(content):
print "Before"
before_say_hello()
ret = func(content)
print "After"
after_say_hello()
return ret
return wrapper
@say_hello_wrapper
def say_hello(content):
print "Hello, {0}".format(content)
return content
if __name__ == "__main__":
ret = say_hello("World")
print ret
结果:
/Users/liudaoqiang/PycharmProjects/numpy/venv/bin/python /Users/liudaoqiang/Project/python_project/day14/decorator_test.py Before before hello Hello, World After after hello World Process finished with exit code 0
老男孩python学习自修第十七天【装饰器】的更多相关文章
- 老男孩python学习自修第十八天【面向对象】
1.类与对象(构造方法与实例化) #!/usr/bin/env python # _*_ coding:UTF-8 _*_ class Province: def __init__(self, nam ...
- python学习笔记(5)--迭代器,生成器,装饰器,常用模块,序列化
生成器 在Python中,一边循环一边计算的机制,称为生成器:generator. 如: >>> g = (x * x for xin range(10)) >>> ...
- python学习笔记-(八)装饰器、生成器&迭代器
本节课程内容概览: 1.装饰器 2.列表生成式&迭代器&生成器 3.json&pickle数据序列化 1. 装饰器 1.1 定义: 本质上是个函数,功能是装饰其他函数—就是为其 ...
- python学习之路 六 :装饰器
本节重点: 掌握装饰器相关知识 python装饰器就是用于拓展原来函数功能的一种函数,这个函数的特殊之处在于它的返回值也是一个函数,使用python装饰器的好处就是在不用更改原函数的代码前提下给函 ...
- python学习总结---函数使用 and 装饰器
# 函数使用 ### 零碎知识 - 灵活的if-else ```python a = 3 if False else 5 print(a) ''' if False: a = 3 else: a = ...
- Python学习日记(七)——装饰器
1.必备知识 #### 一 #### def foo(): print 'foo' foo #表示是函数 foo() #表示执行foo函数 #### 二 #### def foo(): print ' ...
- Python学习基础(三)——装饰器,列表生成器,斐波那契数列
装饰器——闭包 # 装饰器 闭包 ''' 如果一个内部函数对外部(非全局)的变量进行了引用,那么内部函数被认为是闭包 闭包 = 函数块 + 定义时的函数环境 ''' def f(): x = 100 ...
- Python学习笔记(yield与装饰器)
yeild:返回一个生成器对象: 装饰器:本身是一个函数,函数目的装饰其他函数(调用其他函数) 功能:增强被装饰函数的功能 装饰器一般接受一个函数对象作为参数,以便对其增强 @原函数名 来调用其他函 ...
- 老男孩python学习自修第二十四天【多进程】
1. 体验多进程的运行速度 #!/usr/bin/env python # _*_ coding:UTF-8 _*_ from multiprocessing import Pool import t ...
随机推荐
- 学习任务,阅读一下Redis分布式锁的官方文档
地址: https://redis.io/topics/distlock 这是一篇质疑RedLock的论文:https://martin.kleppmann.com/2016/02/08/how-to ...
- 重建UNDO表空间遭遇ORA-01548
今天开发那边的一套数据库的undo表空间不知道被谁设置成了自动扩展,然后所谓的屋漏偏逢连夜雨的是, 开发人员今天跑了一个很大的事物,然后直接后果就是undo表空间不断被扩展,直到把文件系统写爆了.没办 ...
- i春秋-百度杯十月场-EXEC
进入网站,查看源代码,发现是用vim编辑,而抓包没有有效信息,加参数也无果.百度查了一下vim能形成什么文件.找到答案说,用vim编辑文本xxx.php中途退出,会自动创建一个文件.xxx.php.s ...
- Docker网络和容器的通信
Docker的本地网络实现其实就是利用了Linux上的网络命名空间和虚拟网络设备(特别是veth pair). 基本原理 直观上看,要实现网络通信,机器需要至少一个网络接口(物理接口或虚拟接口)与外界 ...
- C# 下载文件
最近一段时间,真的是太忙太忙了!经历了自我毕业以来最忙碌的一个项目! 说起这个项目,我有万千感慨 且不说技术能力,也无需谈论项目需求.单就项目压力,日常加班,周六日补班而言,我相信很多人是扛不住的! ...
- nrf2401 - 最廉价的2.4G无线通信方案
所有的使用Arduino 的朋友大多都会知道大名鼎鼎的XBee 这个土豪级的ZigBee 的通信模块.我们是做产品开发的,对于XBee这个产品可谓是又爱又恨,不得不承认他确实是一个好货,从做工到功能都 ...
- 窥探ASP.Net MVC底层原理 实现跨越Session的分布式TempData
1.问题的引出 我相信大家在项目中都使用过TempData,TempData是一个字典集合,一般用于两个请求之间临时缓存数据或者页面之间传递消息.也都知道TempData是用Session来实现的,既 ...
- RabbitMQ 3.6.1 升级至 3.7.9 版本(Windows 升级至Centos)
随着公司业务量的增加,原本部署在Windows服务器的RabbitMQ集群(3.6.1)总是出现莫名其妙的问题,经查询官方Issue,确认是RabbitMQ 3.6.1 版本的bug.查看从3.6.1 ...
- .NET Core 2.1中的分层编译(预览)
如果您是.NET性能的粉丝,最近有很多好消息,例如.NET Core 2.1中的性能改进和宣布.NET Core 2.1,但我们还有更多的好消息.分层编译是一项重要的新特性功能,我们可以作为预览供任何 ...
- Krpano教程tour.xml详解
<krpano version="1.18" //版本号 onstart="" //网页启动时调用的函数 basedir="%FIRSTXML% ...