Python闭包的高级应用-装饰器的实现
2014-12-27 21:44
579 查看
我们先看一个闭包的例子:
我们先看运行结果:
上面的代码使用了闭包,因为子函数wrapped将父函数的内部变量f与之绑定。
这样,wrapped这个闭包函数,实际上先打印时间,然后调用f,所以正如结果打印的一般,before_call起到的是一种装饰的作用。
这里我扩展它的功能,增加一个调用函数后,打印时间:
运行结果为:
运行结果是正确的。注意最后两个,顺序交换了,对结果无影响。
下面我们再包装一层:
那么如何使用呢?这里就是python装饰器的语法,
如果我们这样使用:
注意test函数前加了装饰的符号。
还可以这样:
甚至可以嵌套多层:
这就是python中装饰器的原理,内部采用了闭包。
from time import ctime def before_call(f): def wrapped(*args, **kargs): print 'before calling, now is %s' % ctime() return f(*args, **kargs) return wrapped def test(name): print 'hello, %s' % (name) if __name__ == '__main__': before_call(test)("lucky")
我们先看运行结果:
~/Documents/py python 2.py before calling, now is Sat Dec 27 21:30:18 2014 hello, lucky
上面的代码使用了闭包,因为子函数wrapped将父函数的内部变量f与之绑定。
这样,wrapped这个闭包函数,实际上先打印时间,然后调用f,所以正如结果打印的一般,before_call起到的是一种装饰的作用。
这里我扩展它的功能,增加一个调用函数后,打印时间:
from time import ctime def before_call(f): def wrapped(*args, **kargs): print 'before calling, now is %s' % ctime() return f(*args, **kargs) return wrapped def after_call(f): def wrapped(*args, **kargs): try: return f(*args, **kargs) finally: print 'after calling, now is %s' % ctime() return wrapped def test(name): print 'hello, %s' % (name) if __name__ == '__main__': before_call(test)("lucky") after_call(test)("peter") before_call(after_call(test))("john") after_call(before_call(test))('marry')
运行结果为:
~/Documents/py python 2.py before calling, now is Sat Dec 27 21:37:24 2014 hello, lucky hello, peter after calling, now is Sat Dec 27 21:37:24 2014 before calling, now is Sat Dec 27 21:37:24 2014 hello, john after calling, now is Sat Dec 27 21:37:24 2014 before calling, now is Sat Dec 27 21:37:24 2014 hello, marry after calling, now is Sat Dec 27 21:37:24 2014
运行结果是正确的。注意最后两个,顺序交换了,对结果无影响。
下面我们再包装一层:
def after_call(): def after(f): def wrapped(*args, **kargs): try: return f(*args, **kargs) finally: print 'after calling, now is %s' % ctime() return wrapped return after def before_call(): def before(f): def wrapped(*args, **kargs): print 'before calling, now is %s' % ctime() return f(*args, **kargs) return wrapped return before
那么如何使用呢?这里就是python装饰器的语法,
如果我们这样使用:
@before_call() def test(name): print 'hello, %s' % (name) if __name__ == '__main__': test("lucky")
注意test函数前加了装饰的符号。
还可以这样:
@after_call() def test(name): print 'hello, %s' % (name)
甚至可以嵌套多层:
@before_call()
@after_call() def test(name): print 'hello, %s' % (name)
这就是python中装饰器的原理,内部采用了闭包。
相关文章推荐
- Python闭包的高级应用-装饰器的实现
- python 闭包与装饰器的实现
- Python高级 -- 11 闭包、装饰器
- Python高级编程--闭包与装饰器
- python -- 装饰器的高级应用
- Python的高级特性7:闭包和装饰器
- python -- 装饰器的高级应用
- NLP︱高级词向量表达(一)——GloVe(理论、相关测评结果、R&python实现、相关应用)
- python核心高级学习总结8------动态性、__slots__、生成器、迭代器、装饰、闭包
- 【Linux的高级应用编程】Linux下的线程实现机制分析
- ASP.NET高级里面的repeater控件的应用实现样式设置,并添加删除按钮
- highcharts 高级应用—动态柱状图的实现
- python 元类型编程,实现匿名验证器的装饰器AuthenticationDecoratorMeta
- python 多个装饰器组合应用,实现面向切面之AOP编程
- highcharts 高级应用—动态柱状图的实现
- highcharts 高级应用—动态曲线的实现
- Python应用-[用Python实现一个socket echo程序 && tcp socket的几个关闭状态]
- python:装饰模式的一个实现
- 线程高级应用-心得5-java5线程并发库中Lock和Condition实现线程同步通讯
- Python 正则表达式高级应用举例