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") 可以看到嵌套函数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__属性,如果它是一个闭包函数,它返回一个单元格对象的元组。 参考上面的例子,我们知道times3和times5是闭包函数。 >>> 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 如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持! (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |