zoukankan      html  css  js  c++  java
  • Python闭包

    在本文中,您将了解什么是Python闭包,如何定义闭包以及应该如何使用闭包。

    嵌套函数中的非局部变量

    在进入闭包之前,我们必须先了解一个嵌套函数和非局部变量。

    在函数中定义另一个函数称为嵌套函数。嵌套函数可以访问包围范围内的变量。

    在Python中,这些非局部变量只能在默认情况下读取,我们必须将它们显式地声明为非局部变量(使用nonlocal关键字)才能进行修改。

    以下是访问非局部变量的嵌套函数的示例。

    def print_msg(msg):
    # This is the outer enclosing function
    
        def printer():
    # This is the nested function
            print(msg)
    
        printer()
    
    # We execute the function
    # Output: Hello
    print_msg("Hello")
    
    Python

    可以看到嵌套函数printer()能够访问封闭函数的非局部变量msg

    定义闭包函数

    在上面的例子中,如果函数print_msg()的最后一行返回printer()函数而不是调用它,会发生什么? 如该函数定义如下 -

    def print_msg(msg):
    # This is the outer enclosing function
    
        def printer():
    # This is the nested function
            print(msg)
    
        return printer  # this got changed
    
    # Now let's try calling this function.
    # Output: Hello
    another = print_msg("Hello")
    another()
    
    Python

    这样是不寻常的。

    print_msg()函数使用字符串“Hello”进行调用,返回的函数被绑定到另一个名称。 在调用another()时,尽管我们已经完成了print_msg()函数的执行,但仍然记住了这个消息。

    一些数据(“Hello”)附加到代码中的这种技术在Python中称为闭包。

    即使变量超出范围或函数本身从当前命名空间中删除,也会记住封闭范围内的值。

    尝试在Python shell中运行以下内容以查看输出。

    >>> del print_msg
    >>> another()
    Hello
    >>> print_msg("Hello")
    Traceback (most recent call last):
    ...
    NameError: name 'print_msg' is not defined
    
    Python

    什么时候闭包?

    从上面的例子可以看出,当嵌套函数引用其封闭范围内的值时,在Python中有使用了一个闭包。

    在Python中创建闭包必须满足的标准将在以下几点 -

    • 必须有一个嵌套函数(函数内部的函数)。
    • 嵌套函数必须引用封闭函数中定义的值。
    • 闭包函数必须返回嵌套函数。

    何时使用闭包?

    那么闭包是什么好的?

    闭包可以避免使用全局值并提供某种形式的数据隐藏。它还可以提供面向对象的解决问题的解决方案。

    当在类中几乎没有方法(大多数情况下是一种方法)时,闭包可以提供一个替代的和更优雅的解决方案。 但是当属性和方法的数量变大时,更好地实现一个类。

    这是一个简单的例子,其中闭包可能比定义类和创建对象更为优先。

    def make_multiplier_of(n):
        def multiplier(x):
            return x * n
        return multiplier
    
    # Multiplier of 3
    times3 = make_multiplier_of(3)
    
    # Multiplier of 5
    times5 = make_multiplier_of(5)
    
    # Output: 27
    print(times3(9))
    
    # Output: 15
    print(times5(3))
    
    # Output: 30
    print(times5(times3(2)))
    
    Python

    Python中的装饰器也可以广泛使用闭包。值得注意的是,可以找到封闭函数中包含的值。

    所有函数对象都有一个__closure__属性,如果它是一个闭包函数,它返回一个单元格对象的元组。 参考上面的例子,我们知道times3times5是闭包函数。

    >>> make_multiplier_of.__closure__
    >>> times3.__closure__
    (<cell at 0x0000000002D155B8: int object at 0x000000001E39B6E0>,)
    
    Python

    单元格(cell)对象具有存储闭合值的属性:cell_contents

    >>> times3.__closure__[0].cell_contents
    3
    >>> times5.__closure__[0].cell_contents
    5
    原文出自【易百教程】,商业转载请联系作者获得授权,非商业转载请保留原文链接:https://www.yiibai.com/python/closure.html

  • 相关阅读:
    常用正则表达式
    C#链接常见数据库的方法
    [转]hibernate分页原理
    2020hdu多校第一场比赛及补题
    2020hdu多校第四场比赛及补题
    2020hdu多校第五场比赛及补题
    2020hdu多校第三场比赛及补题
    2020hdu多校第二场比赛及补题
    第二次vj团队赛补题
    字符串距离问题
  • 原文地址:https://www.cnblogs.com/valorchang/p/11319472.html
Copyright © 2011-2022 走看看