zoukankan      html  css  js  c++  java
  • 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, 闭包,函数对象

  • 相关阅读:
    java后台保存JSON
    查询树节点及其所有上级节点sql语句
    查询树节点及其所有下级节点sql语句
    Hibernate查询机制使用原生sql语法查询
    SSH框架通过poi导出excel表格
    java通过poi导入excel数据
    各类型日期date的相互转化
    推荐一下我喜欢的软件
    青岛市赛总结——远征石油大学
    My learn of git
  • 原文地址:https://www.cnblogs.com/testview/p/4818607.html
Copyright © 2011-2022 走看看