我们先看一个闭包的例子:

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闭包的高级应用-装饰器的实现的更多相关文章

  1. python高级之装饰器

    python高级之装饰器 本节内容 高阶函数 嵌套函数及闭包 装饰器 装饰器带参数 装饰器的嵌套 functools.wraps模块 递归函数被装饰 1.高阶函数 高阶函数的定义: 满足下面两个条件之 ...

  2. 第二篇:python高级之装饰器

    python高级之装饰器   python高级之装饰器 本节内容 高阶函数 嵌套函数及闭包 装饰器 装饰器带参数 装饰器的嵌套 functools.wraps模块 递归函数被装饰 1.高阶函数 高阶函 ...

  3. python中函数总结之装饰器闭包

    1.前言 函数也是一个对象,从而可以增加属性,使用句点来表示属性. 如果内部函数的定义包含了在外部函数中定义的对象的引用(外部对象可以是在外部函数之外),那么内部函数被称之为闭包. 2.装饰器 装饰器 ...

  4. 简学Python第四章__装饰器、迭代器、列表生成式

    Python第四章__装饰器.迭代器 欢迎加入Linux_Python学习群  群号:478616847 目录: 列表生成式 生成器 迭代器 单层装饰器(无参) 多层装饰器(有参) 冒泡算法 代码开发 ...

  5. Python全栈开发之---装饰器

    1.装饰器的形成过程 import time def func1(): print('in func1') def timer(func): def inner(): start = time.tim ...

  6. 初学 Python(十五)——装饰器

    初学 Python(十五)--装饰器 初学 Python,主要整理一些学习到的知识点,这次是生成器. #-*- coding:utf-8 -*- import functools def curren ...

  7. 十一. Python基础(11)—补充: 作用域 & 装饰器

    十一. Python基础(11)-补充: 作用域 & 装饰器 1 ● Python的作用域补遗 在C/C++等语言中, if语句等控制结构(control structure)会产生新的作用域 ...

  8. Python 函数修饰符(装饰器)的使用

     Python 函数修饰符(装饰器)的使用 1.  修饰符的来源修饰符是一个很著名的设计模式,经常被用于有切面需求的场景,较为经典的有插入日志.性能测试.事务处理等. 修饰符是解决这类问题的绝佳设计, ...

  9. Python 标准库中的装饰器

    题目描述 1.简单举例 Python 标准库中的装饰器 2.说说你用过的 Python 标准库中的装饰器 1. 首先,我们比较熟悉,也是比较常用的 Python 标准库提供的装饰器有:property ...

随机推荐

  1. 正则表达式、re、常用模块

    阅读目录 正则表达式 字符 量词 . ^ $ * + ? { } 字符集[][^] 分组 ()与 或 |[^] 转义符 \ 贪婪匹配 re 总结 正则 re 常用模块 namedtuple deque ...

  2. python 执行顺序

    从上往下顺序执行,定义的方法和类要写在调用之前, 如果有 if __name__ == '__main__'   改方法所在的文件作为启动文件时会被调用,如果作为模块被调用时不会被执行.

  3. 奶牛与农夫John与oj

    当蒟蒻的我悲惨的发现oj出现大量的奶牛与农夫时,觉得早晚usaco要占领oj,于是绝望的开始记录农夫与奶牛的题目……. 一道usaco月赛的题…在oj用作练习二维数组,虽然数据的大量字符确实很让人不爽 ...

  4. 训练指南 UVALive - 3415(最大点独立集)

    layout: post title: 训练指南 UVALive - 3415(最大点独立集) author: "luowentaoaa" catalog: true mathja ...

  5. 洛谷——P1033 自由落体

    P1033 自由落体 题目描述 在高为 H 的天花板上有 n 个小球,体积不计,位置分别为 0,1,2,….n-1.在地面上有一个小车(长为 L,高为 K,距原点距离为 S1).已知小球下落距离计算公 ...

  6. [BZOJ3786]星系探索(伪ETT)

    3786: 星系探索 Time Limit: 40 Sec  Memory Limit: 256 MBSubmit: 1638  Solved: 506[Submit][Status][Discuss ...

  7. [Markdown]纯文本标记语言MarkdowPad2--MD语法知识

    ##1.标题 代码 注:# 后面保持空格 # h1 ## h2 ### h3 #### h4 ##### h5 ###### h6 ####### h7 // 错误代码 ######## h8 // ...

  8. 统计正数和负数的个数,然后计算这些数的平均值 Exercise05_01

    import java.util.Scanner; /** * @author 冰樱梦 * * */ public class Exercise05_01{ public static void ma ...

  9. Sublime | 编辑工具Sublime的使用小结

    文章目录 Sublime Package & Usage MarkdownEditing MarkdownPreview Usage Key Bindings Setting (语法高亮和ma ...

  10. INLINE-BLOCK和FLOAT(二)(转)

    一.一抹前言 没有爱的日子,时间如指尖细沙,不知不觉就流逝了.写“CSS float浮动的深入研究.详解及拓展(一)”和“CSS float浮动的深入研究.详解及拓展(二)”似乎就在不久前,然而相隔差 ...