手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

Python闭包与函数对象

[日期:2015-09-19] 来源:Linux社区  作者:testview [字体: ]

1. Python闭包是什么

在python中有函数闭包的概念,这个概念是什么意思呢,查看Wikipedia的说明如下:

In programming languages, closures (also lexical closures or function closures) are a technique for implementing lexically scoped name binding in languages with first-class functions. Operationally, a closure is a record storing a function[a] together with an environment:[1] a mapping associating each free variable of the function (variables that are used locally, but defined in an enclosing scope) with the value or storage location to which the name was bound when the closure was created.[b] A closure—unlike a plain function—allows the function to access those captured variables through the closure's reference to them, even when the function is invoked outside their scope.

看上去概念很多,下面我们通俗的讲一下

假设我有个求x^n的函数如下

def pow(x, n):
    res = 1
    for i in range(n):
        res *= x
    return res

(例1)

在某一段代码里,我总是用到平方和(比如求矩形对角线的时候),那我的代码是

len2d = pow(20,2) + pow(30,2)

这时候我希望第二个参数总是取2,不用重复写

在另一段代码里,我总是用到立方和(比如求正方体对角线的时候),那我的代码又变成

len3d = pow(20,3) + pow(30,3) + pow(40,3)

这个时候我希望第二个参数固定为3

在上面这两种情况里,函数闭包就有了用武之地:

def pown(n):
    def pow(x):
        res = 1
        for i in range(n):    # 引用外围函数状态
            res *= x
        return res
    return pow


pow2 = pown(2)
len2d = pow2(20) + pow2(30)
 
pow3 = pown(3)
len3d = pow3(20) + pow3(30) + pow3(40)

(例2)

从例2我们看到,pown是外围函数,它传入了一个参数n,并且返回了一个内部函数。pow就是python中的闭包函数,它不但有自己的执行逻辑,也能引用到参数n。

这就是闭包函数和普通函数最大的不同,闭包函数除了函数执行体,还“闭包”了外围状态。每个闭包函数实例都能“闭包”各自的状态。

2. 闭包和函数对象

如果要把闭包和c++做个对比,应该类似于c++中的函数对象。函数对象用python来实现的代码如下:

class Pow(object):
    def __init__(self, n):
        self.n = n
    def __call__(self, x):
        res = 1
        for i in range(self.n):    # 引用对象成员
            res *= x
        return res

pow2 = Pow(2)
len2d = pow2(20) + pow2(30)

pow3 = Pow(3)
len3d = pow3(20) + pow3(30) + pow3(40)

(例3)

例3的类中定义了特殊方法__call__,因此它的对象能被直接做函数调用,称之为函数对象。由于它是一个对象,因此在初始化的时候可以传入参数进行保存,这点就类似于之前提到的闭包的概念。

从这个类比来看,闭包可以近似的看成是简化的函数对象

下面关于Python的文章您也可能喜欢,不妨看看:

Python:在指定目录下查找满足条件的文件  http://www.linuxidc.com/Linux/2015-08/121283.htm

Python2.7.7源码分析  http://www.linuxidc.com/Linux/2015-08/121168.htm

无需操作系统直接运行 Python 代码  http://www.linuxidc.com/Linux/2015-05/117357.htm

CentOS上源码安装Python3.4  http://www.linuxidc.com/Linux/2015-01/111870.htm

《Python核心编程 第二版》.(Wesley J. Chun ).[高清PDF中文版] http://www.linuxidc.com/Linux/2013-06/85425.htm

《Python开发技术详解》.( 周伟,宗杰).[高清PDF扫描版+随书视频+代码] http://www.linuxidc.com/Linux/2013-11/92693.htm

Python脚本获取Linux系统信息 http://www.linuxidc.com/Linux/2013-08/88531.htm

Ubuntu下用Python搭建桌面算法交易研究环境 http://www.linuxidc.com/Linux/2013-11/92534.htm

Python 语言的发展简史 http://www.linuxidc.com/Linux/2014-09/107206.htm

Python 的详细介绍请点这里
Python 的下载地址请点这里 

本文永久更新链接地址http://www.linuxidc.com/Linux/2015-09/123287.htm

linux
相关资讯       Python闭包  Python函数对象 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款