Problem
Consider:
>>> squares = []
>>> for x in range(5):
... squares.append(lambda: x**2)
>>> squares[2]()
>>> squares[4]()
It will return:
>>> squares[2]()
16
>>> squares[4]()
16
This happens because x
is not local to the lambdas, but is defined in the outer scope, and it is accessed when the lambda is called — not when it is defined. At the end of the loop, the value of x
is 4, so all the functions now return 4**2, i.e. 16.