Python 简明教程

Python - Closures

What is a Closure?

Python 闭包是一个嵌套函数,它可以访问已完成执行的封闭函数中的一个变量。此类变量未绑定到本地作用域。若要使用不可变变量(数字或字符串),我们必须使用 non-local 关键字。

A Python closure is a nested function which has access to a variable from an enclosing function that has finished its execution. Such a variable is not bound in the local scope. To use immutable variables (number or string), we have to use the non-local keyword.

Python 闭包的主要优点是,我们可以帮助避免使用全局值并提供某种形式的数据隐藏。它们用于 Python 装饰器。

The main advantage of Python closures is that we can help avoid the using global values and provide some form of data hiding. They are used in Python decorators.

闭包与嵌套函数密切相关,允许内部函数在外部函数完成执行后捕获和保留封闭函数的局部状态。理解闭包需要了解嵌套函数、变量作用域以及 Python 如何处理函数对象。

Closures are closely related to nested functions and allow inner functions to capture and retain the enclosing function’s local state, even after the outer function has finished execution. Understanding closures requires familiarity with nested functions, variable scope and how Python handles function objects.

  1. Nested Functions: In Python functions can be defined inside other functions. These are called nested functions or inner functions.

  2. Accessing Enclosing Scope: Inner functions can access variables from the enclosing i.e. outer scope. This is where closures come into play.

  3. Retention of State: When an inner function i.e. closure captures and retains variables from its enclosing scope, even if the outer function has completed execution or the scope is no longer available.

Nested Functions

Python 中的嵌套函数是指在一个函数中定义另一个函数的做法。此概念可让我们更有效地组织代码,封装功能并管理变量作用域。

Nested functions in Python refer to the practice of defining one function inside another function. This concept allows us to organize code more effectively, encapsulate functionality and manage variable scope.

以下是嵌套函数的示例,其中 functionB 在 functionA 中定义。然后,从外层函数的作用域中调用内部函数。

Following is the example of nested functions where functionB is defined inside functionA. Inner function is then called from inside the outer function’s scope.

Example

def functionA():
   print ("Outer function")
   def functionB():
      print ("Inner function")
   functionB()

functionA()

Output

Outer function
Inner function

如果外层函数接收到任何参数,它可按以下示例所示传递给内部函数。

If the outer function receives any argument, it can be passed to the inner function as in the below example.

def functionA(name):
   print ("Outer function")
   def functionB():
      print ("Inner function")
      print ("Hi {}".format(name))
   functionB()

functionA("Python")

Output

Outer function
Inner function
Hi Python

Variable Scope

当闭包创建(即,捕获来自其包围作用域的变量的内部函数)时,即使外层函数已经执行完毕,它仍可保留对那些变量的访问。此行为可让闭包“记住”并操作来自包围作用域的变量的值。

When a closure is created i.e. an inner function that captures variables from its enclosing scope, it retains access to those variables even after the outer function has finished executing. This behavior allows closures to "remember" and manipulate the values of variables from the enclosing scope.

Example

以下是包含可变作用域的闭包的示例 −

Following is the example of the closure with the variable scope −

def outer_function(x):
    y = 10

    def inner_function(z):
        return x + y + z  # x and y are captured from the enclosing scope

    return inner_function

closure = outer_function(5)
result = closure(3)
print(result)

Output

18

Creating a closure

创建 Python 中的闭包包括定义外层函数中的嵌套函数并返回内部函数。闭包可用于捕获和保留来自包围作用域的变量的状态。

Creating a closure in Python involves defining a nested function within an outer function and returning the inner function. Closures are useful for capturing and retaining the state of variables from the enclosing scope.

Example

在以下示例中,我们有一个 functionA 函数,其创建一个并返回另一个函数 functionB 。嵌套 functionB 函数是闭包。

In the below example, we have a functionA function which creates and returns another function functionB. The nested functionB function is the closure.

外层 functionA 函数返回 functionB 函数,并将其指定给 myfunction 变量。即便它已经执行完毕。然而,printer 闭包仍然有权访问 name 变量。

The outer functionA function returns a functionB function and assigns it to the myfunction variable. Even if it has finished its execution. However, the printer closure still has access to the name variable.

以下是创建 Python 中闭包的示例 −

Following is the example of creating the closure in python −

def functionA(name):
   name ="New name"
   def functionB():
      print (name)
   return functionB

myfunction = functionA("My name")
myfunction()

Output

New name

nonlocal Keyword

在 Python 中,nonlocal 关键字允许访问位于局部作用域之外的变量。这在闭包中用于修改存在于外层变量作用域中的不可变变量。以下是包含 nonlocal 关键字的闭包示例。

In Python, nonlocal keyword allows a variable outside the local scope to be accessed. This is used in a closure to modify an immutable variable present in the scope of outer variable. Here is the example of the closure with the nonlocal keyword.

def functionA():
   counter =0
   def functionB():
      nonlocal counter
      counter+=1
      return counter
   return functionB

myfunction = functionA()

retval = myfunction()
print ("Counter:", retval)

retval = myfunction()
print ("Counter:", retval)

retval = myfunction()
print ("Counter:", retval)

Output

Counter: 1
Counter: 2
Counter: 3