简体   繁体   English

非本地 function 如何在 Python 中存储值

[英]how does nonlocal function store values in Python

I'm confused about how the get() function works.我对 get() function 的工作原理感到困惑。 How can it read the previous values?它如何读取以前的值? Are the values are stored in f function?这些值是否存储在 f function 中? How?如何?

def nonlocalist():
    get = lambda x:'Index out of range!'
    def prepend(value):
        nonlocal get
        f = get
        def get(i):
            if i == 0:
                return value
            return f(i - 1)
    return prepend, lambda x: get(x)

prepend, get = nonlocalist()
prepend(2)
prepend(3)
prepend(4)

get(2)

The values are stored in function objects.这些值存储在 function 对象中。 Each call to prepend creates a new get function object that refers to the preceding get function object, which still exists, and each get function object that prepend creates stores the value parameter passed to prepend . Each call to prepend creates a new get function object that refers to the preceding get function object, which still exists, and each get function object that prepend creates stores the value parameter passed to prepend .

When nonlocalist is called, get refers to the lambda function that returns the 'Index out of range.'当调用nonlocalist时, get指的是返回“索引超出范围”的 lambda function。 message.信息。 Let's call this get0 .我们称之为get0 (This term is defined just for this discussion. It doesn't mean anything in the code itself.) (这个术语只是为了这个讨论而定义的。它在代码本身中没有任何意义。)

Then when prepend is called after that, it sets f to refer to get0 .然后,在此之后调用prepend时,它将f设置为引用get0 Then it redefines get and the new get function object has the value passed to prepend bound into it.然后它重新定义get并且新的get function object 将传递的value prepend绑定到它。 We'll call this new get function get1 .我们称这个新的get function get1 Now if get1 is called with a parameter of zero, it will return the value passed to prepend .现在,如果使用零参数调用get1 ,它将返回传递给prepend的值。 If it gets called with any other parameter, it will call f , which refers to get0 , with a parameter of zero, and that function, as we have seen, will return the error message regardless of its parameter.如果使用任何其他参数调用它,它将调用f ,它指的是get0 ,参数为零,并且正如我们所见,function 将返回错误消息,而不管其参数如何。

At this point, the get1 function is around because it was just defined and the get0 function is also still around because get1 holds a reference to it as f .此时, get1 function 存在,因为它刚刚被定义,而get0 function 也仍然存在,因为get1将其引用为f

The next call to prepend generates a new function object get2 , which stores that prepend call's value and calls get1 (as f ).prepend的下一个调用会生成一个新的 function object get2 ,它存储该prepend调用的value并调用get1 (作为f )。 When it calls f , it subtracts 1 from the subscript argument i , so i controls how far the f calls travel down the list.当它调用f时,它会从下标参数i中减去 1,因此i控制f调用在列表中传播的距离。

So each call to prepend generates a new and different get n function object that contains a value from prepend and a reference to the preceding get (n - 1) function, all the way back to get0 .因此,每次对prepend的调用都会生成一个新的不同的get n function object ,其中包含来自prependvalue和对前面get (n - 1) get0的引用。

In conclusion, I must say: that is one gnarly nest of functions.总之,我必须说:这是一个粗糙的函数巢。

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

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