Python基础 专题
您的位置:python > Python基础专题 > Python闭包
Python闭包
作者:--    发布时间:2019-11-20

在本文中,您将了解什么是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")

可以看到嵌套函数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()

这样是不寻常的。

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中创建闭包必须满足的标准将在以下几点 -

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

何时使用闭包?

那么闭包是什么好的?

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

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

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

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中的装饰器也可以广泛使用闭包。值得注意的是,可以找到封闭函数中包含的值。

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

>>> make_multiplier_of.__closure__
>>> times3.__closure__
(<cell at 0x0000000002d155b8: int object at 0x000000001e39b6e0>,)

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

>>> times3.__closure__[0].cell_contents
3
>>> times5.__closure__[0].cell_contents
5

网站声明:
本站部分内容来自网络,如您发现本站内容
侵害到您的利益,请联系本站管理员处理。
联系站长
373515719@qq.com
关于本站:
编程参考手册