Please help me understand this code behind the scenes:
def someFunction(): list = [] for i in range(5): list.append(lambda x: i ** x) return list #вызов функции list = someFunction() print(list[1](2)) In theory, the result of this code should be one, but in fact 16. is displayed. I do not quite understand how this program works. To my mind:
The compiler starts with the 1st line. See def and skip the entire function. Then gets to the string
list = someFunction ()
after which begins to interpret the function someFunction . It gets to the for loop, and the first iteration begins. But the command that adds an element to the end of the list takes a function as an argument, which, in turn, has not yet received its argument. What is added to the list? I assume that a function object has been created that is associated with a specific list item. So, I think, happens at all iterations. After that, the function returns a list containing references to the created function objects. If everything is as it really is, then this is logical.
But what happens when reading an item from the list? Okay. If it comes up 16 instead of 1, most likely, all the objects of the functions take 4-ku as an iteration variable, i.e. last iteration. And here it is not entirely clear why when creating the function object the value of the loop counter was not saved? I suppose that you can tell me: "And how do you think this value will remain if it is not in the parameters of the lambda function?". But then how does she find and substitute the last iteration? And in general, while writing this post is even more confused: why would this compiler create an object function before this function is called? After all, the object of the function someFunction was constructed when we called this function. And the objects of the lambda function were created before they were called.
Be kind, help sort out this issue. Thank.