Let us again consider our two simple function definitions and illustrate the process that evaluates a call expression for a user-defined function.

    让我们再次考虑两个简单的函数定义,并演示计算用户定义函数的调用表达式的过程。

    image.png
    Python first evaluates the name sum_squares, which is bound to a user-defined function in the global frame. The primitive numeric expressions 5 and 12 evaluate to the numbers they represent.

    Python首先计算名称sum_squares,它被绑定到全局帧中的一个用户定义的函数。基本数字表达式5和12计算它们所代表的数字。

    Next, Python applies sum_squares, which introduces a local frame that binds x to 5 and y to 12.

    接下来,Python应用sum_squares,它引入了一个局部帧,将x绑定到5,y绑定到12。

    image.png
    The body of sum_squares contains this call expression:

    sum_squares的主体包含以下调用表达式:

    add ( square(x) , square(y) )
    _ _ _
    operator operand 0 operand 1
    All three subexpressions are evaluated in the current environment, which begins with the frame labeled sum_squares(). The operator subexpression add is a name found in the global frame, bound to the built-in function for addition. The two operand subexpressions must be evaluated in turn, before addition is applied. Both operands are evaluated in the current environment beginning with the frame labeled sum_squares.

    所有三个子表达式都在当前环境中进行计算,当前环境从标记为sum_squares()的帧开始。运算符子表达式add是全局帧中的一个名称,绑定到用于加法的内置函数。在应用加法之前,必须依次计算两个操作数子表达式。两个操作数都在当前环境标有sum_squares的帧开始计算。

    In operand 0, square names a user-defined function in the global frame, while x names the number 5 in the local frame. Python applies square to 5 by introducing yet another local frame that binds x to 5.

    operand 0中,square在全局帧中命名一个用户定义的函数,而x在局部帧中命名数字5。Python通过引入另一个将x绑定到5的局部帧,将square应用于5

    image.png
    Using this environment, the expression mul(x, x) evaluates to 25.

    使用这种环境,表达式mul(x,x)的计算结果为25。

    Our evaluation procedure now turns to operand 1, for which y names the number 12. Python evaluates the body of square again, this time introducing yet another local frame that binds x to 12. Hence, operand 1 evaluates to 144.

    我们的计算过程现在转向operand 1y为其命名数字12。Python再次计算square的主体,这一次引入了另一个将x绑定到12的局部帧。因此,operand 1的计算结果为144。

    image.png
    Finally, applying addition to the arguments 25 and 144 yields a final return value for sum_squares: 169.

    最后,将参数25和144相加得到sum_squares的最终返回值:169。

    image.png
    This example illustrates many of the fundamental ideas we have developed so far. Names are bound to values, which are distributed across many independent local frames, along with a single global frame that contains shared names. A new local frame is introduced every time a function is called, even if the same function is called twice.

    这个例子说明了我们迄今为止所发展的许多基本思想。名称与值绑定在一起,这些值分布在许多独立的局部帧中,同时还有一个包含共享名称的全局帧。每次调用一个函数时,都会引入一个新的本地帧,即使同一个函数被调用两次。

    All of this machinery exists to ensure that names resolve to the correct values at the correct times during program execution. This example illustrates why our model requires the complexity that we have introduced. All three local frames contain a binding for the name x, but that name is bound to different values in different frames. Local frames keep these names separate.

    所有这些机制的存在是为了确保名称在程序执行期间的正确时间解析为正确的值。这个例子说明了为什么我们的模型需要我们引入的复杂性。所有三个局部帧都包含名称 x 的绑定,但该名称绑定到不同帧中的不同值。局部帧将这些名字分开。