Python闭包与函数对象

本文介绍了Python中的闭包概念,通过具体示例展示了如何使用闭包简化代码并保持良好的可读性和可维护性。同时,文章将闭包与函数对象进行了对比,加深读者对闭包的理解。

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.

” —— 原文链接:https://en.wikipedia.org/wiki/Closure_(computer_programming)

 

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

假设我有个求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, 闭包,函数对象

转载于:https://www.cnblogs.com/testview/p/4818607.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值