When diving into the intricacies of lexical closures, a peculiar behavior in Python code becomes apparent. Consider the following example:
flist = [] for i in xrange(3): def func(x): return x * i flist.append(func) for f in flist: print f(2)
To our surprise, this code prints "4 4 4" instead of the expected "0 2 4". This unexpected behavior stems from the way Python handles lexical closures.
Unlike in Perl, which creates a new closure for each iteration, Python creates three separate functions. However, each function retains the closure of the environment in which it was defined, namely the global environment. This shared closure results in every function referencing the same mutable variable i, leading to the unexpected output.
To address this issue, a modified solution must be employed. By introducing a function creator and invoking it inside the loop, we create distinct environments for each function with a unique i value:
flist = [] for i in xrange(3): def funcC(j): def func(x): return x * j return func flist.append(funcC(i)) for f in flist: print f(2)
This revised code rectifies the problem, demonstrating the subtle but significant differences in how Python and Perl handle lexical closures. By understanding the closure behavior of the specific programming language being used, developers can avoid such unexpected outcomes and write more robust and predictable code.
The above is the detailed content of Why Does Python\'s Closure Behavior Produce Unexpected Results in Nested Loops?. For more information, please follow other related articles on the PHP Chinese website!