python闭包
To understand the concept of closures in python we must know what are nested functions and non-loc al variables. So let's start with them first.
要了解python中闭包的概念,我们必须知道什么是嵌套函数和非局部变量。 因此,让我们先从它们开始。
嵌套函数和非局部变量 (Nested functions and Non-local variables)
When a function is defined inside another function then this is called nesting of functions, where, the function inside which another function is defined is called the outer function and the function which is defined inside another function is called the inner function. An example depicting the use of nested functions is shown below:
当在另一个函数内部定义一个函数时,这称为函数嵌套,其中在其中定义另一个函数的函数称为外部函数 ,而在另一个函数内部定义的函数称为内部函数 。 下面显示了一个描述嵌套函数用法的示例:
#defining nested function
def outer(message):
#text is having the scope of outer function
text = message
def inner():
#using non-local variable text
print(text)
#calling inner function
inner()
# main method
if __name__=='__main__':
outer('Hello!')
Hello!
你好!
In the above code snippet, the inner function inner()
is able to access the local variable text
of the outer function outer()
which has a scope that extends up to the whole body of the outer()
function. The local variable text
of the outer function is a non-local variable for the inner function which it can access but not modify.
在上面的代码片段中,内部函数inner()
能够访问外部函数outer()
的局部变量text
,该局部变量text
的范围一直扩展到outer()
函数的整个主体。 外部函数的局部变量text
是内部函数的非局部变量 ,可以访问但不能修改。
了解关闭的概念 (Understanding the concept of Closures)
A closure is a function object (a function that behaves like an object) that remembers values in enclosing scopes even if they are not present in memory.
闭包是一个函数对象(功能类似于对象的函数),即使在内存中不存在值,它也会记住范围内的值。
When a function is available inside another function then closure is created. The inner function will have access to variables and parameters of outer function even after outer function is returned. This means that we can call the inner function later and it will have access to variables and parameters of outer function. Actually, the closure has reference to the variables and parameters of outer function. We can say that, closure is a record that stores a function together with an environment.
当一个函数在另一个函数内部可用时,将创建闭包。 即使在返回外部函数之后,内部函数也可以访问外部函数的变量和参数。 这意味着我们可以稍后调用内部函数,它将可以访问外部函数的变量和参数。 实际上,闭包引用了外部函数的变量和参数。 可以说,闭包是将功能与环境存储在一起的记录。
#defining nested function
def outer(message):
#text is having the scope of outer function
text = message
def inner():
#using non-local variable text
print(text)
#return inner function
return inner
# main method
if __name__=='__main__':
func = outer('Hello!')
func()
Hello!
你好!
In the above example, we have used a closure to access inner()
function out of its scope, as the inner()
function is only available inside the outer()
function but by returning it, we can access it outside the outer()
function.
在上面的例子中,我们使用一个封闭件来访问inner()
函数出其范围,作为inner()
函数是仅可用的内部outer()
函数,但通过返回它,我们可以外部访问它outer()
功能。
Then in the main method, we have called the outer()
function and returned the inner()
function reference to the func
variable.
然后在main方法中,我们调用了outer()
函数,并将inner()
函数引用返回给func
变量。
Now, the func
variable has reference to the inner()
function which means when we use parentheses with the func
variable then it works as inner()
function which is accessing the text
variable of outer()
function that was called at the time of the declaration of func
variable.
现在, func
变量引用了inner()
函数,这意味着当我们在func
变量中使用括号时,它将作为inner()
函数,该函数访问在调用时调用的outer()
函数的text
变量。 func
变量的声明。
Let's take one more example:
让我们再举一个例子:
This example is a little more complex, as here we have used arguments in the inner operation(n)
function. So, when the inrementor(m)
function is called, it returns the reference to operation(n)
function.
这个示例稍微复杂一点,因为这里我们在内部operation(n)
函数中使用了参数。 因此,当inrementor(m)
函数时,它将引用返回给operation(n)
函数。
And while defining the variables incrementby1
, incrementby5
and incrementby9
, we have passed the value of m
argument every time.
并且在定义变量incrementby1
, incrementby5
和incrementby9
,我们每次都传递了m
参数的值。
Hence, the variables incrementby1
, incrementby5
and incrementby9
refer to the operation(n)
function with the value of m
argument set as 1, 5 and 9 respectively.
因此,变量incrementby1
, incrementby5
和incrementby9
指operation(n)
函数的值m
分别1,5和9的参数集。
And then while calling the operation(n)
function using the variables incrementby1
, incrementby5
and incrementby9
, we have also passed the value of n
and hence got the desired outputs.
然后,在使用变量incrementby1
, incrementby5
和incrementby9
调用operation(n)
函数时,我们还传递了n
的值,因此获得了所需的输出。
有关关闭的重要注意事项 (Important Points to Remember about Closures)
Following are some useful points which also form necessary conditions for implementing closures in python:
以下是一些有用的观点,这些观点也构成了在python中实现闭包的必要条件:
There should be nested function i.e. function inside a function.
应该有嵌套函数,即函数内部的函数。
The inner function must refer to a non-local variable or the local variable of the outer function.
内部函数必须引用非局部变量或外部函数的局部变量。
The outer function must return the inner function.
外部函数必须返回内部函数。
何时使用闭包? (When to use Closures?)
Following are some use cases when closures should be used in python.
以下是在python中使用闭包时的一些用例。
Closures can avoid the use of global values.
闭包可以避免使用全局值。
It provides some form of data hiding.
它提供某种形式的数据隐藏。
When there are few methods (one method in most cases) to be implemented in a class, closures can provide a better solution. But when the number of attributes and methods are more, it is better to implement a class.
当在一个类中实现的方法很少(大多数情况下是一个方法)时,闭包可以提供更好的解决方案。 但是,当属性和方法的数量更多时,最好实现一个类。
python闭包