简体   繁体   English

什么是封闭功能?

[英]What are enclosing functions?

According to the python tutorial , functions look for variable names in the symbol tables of enclosing functions before looking for global functions: 根据python教程 ,函数在查找全局函数之前在封闭函数的符号表中查找变量名:

The execution of a function introduces a new symbol table used for the local variables of the function. 函数的执行引入了用于函数局部变量的新符号表。 More precisely, all variable assignments in a function store the value in the local symbol table; 更确切地说,函数中的所有变量赋值都将值存储在本地符号表中; whereas variable references first look in the local symbol table, then in the local symbol tables of enclosing functions, then in the global symbol table, and finally in the table of built-in names. 而变量引用首先在本地符号表中查找,然后在封闭函数的本地符号表中查找,然后在全局符号表中查找,最后在内置名称表中查找。 Thus, global variables cannot be directly assigned a value within a function (unless named in a global statement), although they may be referenced. 因此,全局变量不能直接在函数内赋值(除非在全局语句中命名),尽管可以引用它们。

What exactly does "enclosing function" mean, and when is it used? “封闭功能”究竟是什么意思,何时使用?

I see the following code prints 10 when called 我看到以下代码在调用时打印10

def parent_function():
    y=10
    def child_function():
        print y
    child_function()

However, calling child_function() alone produces an error. 但是,单独调用child_function()会产生错误。 Are enclosing functions used frequently? 是否经常使用封闭功能?

The concept of an enclosing function is key in understanding the idea of closures. 封闭函数的概念是理解闭包概念的关键。 Because python does not have fully featured lambdas (they only allow expressions and not statements), having nested functions to pass on to other functions is a common use case: 因为python没有全功能的lambdas(它们只允许表达式而不是语句),所以将嵌套函数传递给其他函数是一个常见的用例:

def receiving_function(f):
    f()

def parent_function():
    y = 10

    def child_function():
        print(y)

    receiving_function(child_function)

will print 10 as before. 将像以前一样打印10 This is a common instance of a closure, where the enclosing function "hands off" it's variables to the enclosed function. 这是闭包的常见实例,其中封闭函数将其变量“移交”到封闭函数中。 In the example above, this function is passed off to the receiving_function along with the non-local variable y . 在上面的示例中,此函数与非局部变量y一起传递给receiving_function

The reason you cannot call child_function by itself, is that it is defined inside the parent_function . child_function调用child_function的原因是它是在parent_function定义的。 All python variable declarations use block scope, and declaring a function is no different. 所有python变量声明都使用块作用域,声明一个函数也没有什么不同。

Consider the following example. 请考虑以下示例。

>>> def parent_function():
...    y=10
...    def child_function():
...        print y
...    child_function()

>>> print y
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
NameError: name 'y' is not defined

The variable y is not accessible outside the parent_function . 变量y不能在parent_function之外parent_function Why would you expect that child_function would be any different that y ? 为什么你会期望child_functiony有什么不同呢?

Enclosing functions are functions nested in functions. 封闭函数是嵌套在函数中的函数。 We usually use it to get better encapsulation. 我们通常使用它来获得更好的封装。 That is enclosing functions are not visible for other functions. 这是封闭功能对其他功能不可见。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM