老男孩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 ...
随机推荐
- .NET 环境中使用RabbitMQ 转发 http://www.cnblogs.com/yangecnu/p/4227535.html
.NET 环境中使用RabbitMQ 在企业应用系统领域,会面对不同系统之间的通信.集成与整合,尤其当面临异构系统时,这种分布式的调用与通信变得越发重要.其次,系统中一般会有很多对实时性要求不高的 ...
- AI Haar特征
Haar特征,也叫矩形特征,有四种特征(模板):边缘特征.线性特征.中心特征.对角线特征.每种模板都包含黑白两种区域. 模板的特征值=白色区域的像素和-黑色区域的像素和,反映的是图像的灰度变化情况. ...
- Lucene.Net如何实现搜索结果分类统计功能
最近我们搜易站内搜索系统的一个客户需要一个无限级分类和分类统计功能,要实现的效果如下: 但由于搜易站内搜索系统是基于Lucene.net 2.0开发的,并没有内置的分类统计搜索功能,于是乎只能自己实现 ...
- Item 22: 当使用Pimpl机制时,在实现文件中给出特殊成员函数的实现
本文翻译自<effective modern C++>,由于水平有限,故无法保证翻译完全正确,欢迎指出错误.谢谢! 博客已经迁移到这里啦 如果你曾经同过久的编译时间斗争过,那么你肯定对Pi ...
- 自定义分页及Cookie、Session机制
分页 自定义分页 data = [] , ): tmp = {"id": i, "name": "alex-{}".format(i)} d ...
- python第三章:循环语句--小白博客
Python条件语句 Python条件语句是通过一条或多条语句的执行结果(True或者False)来决定执行的代码块. 可以通过下图来简单了解条件语句的执行过程: Python程序语言指定任何非0和非 ...
- Python学习第六篇——字典中的键和值
favorite_language ={ "jen":"python", "sarah":"c", "edwa ...
- C#使用OneNote的图片文字识别功能(OCR)
http://www.cnblogs.com/Charltsing/p/OneNoteOCR.html 有需要技术咨询的,联系QQ564955427 前段时间有人问我能不能通过OneNote扫描图片, ...
- Django 内的母版-子html规则
一.母版 在实际应用中,在开发一个网站时,从首页到主页.到目录页,等等!有时候,我们大部分基础网页头.边框.侧边框.基础css.js等复用性很高,如果每一个html都要独立去写的话,就太麻烦了. 而把 ...
- python中类方法,实例方法,静态方法的作用和区别
Python中至少有三种比较常见的方法类型,即实例方法,类方法.静态方法.它们是如何定义的呢?如何调用的呢?它们又有何区别和作用呢?且看下文. 首先,这三种方法都定义在类中.下面我先简单说一下怎么 ...