Lexical Closures in Python: Understanding the Unexpected Behavior
In Python, lexical closures allow inner functions to access variables defined in the enclosing scope. However, an unexpected behavior arises when defining functions within a loop that modifies a shared variable. To illustrate this, consider the following Python code:
flist = [] for i in range(3): def func(x): return x * i flist.append(func) for f in flist: print(f(2))
This code prints "4 4 4," which is surprising as one might expect "0 2 4." The reason for this unexpected behavior lies in the closure mechanism. In Python, functions defined within loops create new functions but share the enclosing scope, which in this case is the global scope. As a result, when the value of i is subsequently modified, all the functions within the list refer to the same modified i value.
To address this issue, one needs to create unique environments for each function within the loop. This can be accomplished by using a function creater:
flist = [] for i in range(3): def funcC(j): def func(x): return x * j return func flist.append(funcC(i)) for f in flist: print(f(2))
In this revised code, each call to funcC generates a new closure environment with its own value of i. As a result, each function in the list has access to a distinct i value, leading to the expected output of "0 2 4."
This behavior underscores the importance of understanding how closures work in Python, especially when dealing with side effects and functional programming. When functions defined within loops share a modified variable, unexpected behavior can arise. Utilizing function creators helps create unique environments for each function and ensures the desired behavior is achieved.
The above is the detailed content of Why Do Python Lexical Closures in Loops Produce Unexpected Results?. For more information, please follow other related articles on the PHP Chinese website!