Mar-08-2017, 08:29 PM
Python uses "late binding" here. Lambda is defined with variable i, but actual value of i is looked up when that lambda is called. After for loop i is 2, so all lambdas return 2.
Common trick to avoid this is to use such variable as another parameter:
Common trick to avoid this is to use such variable as another parameter:
lambda x, i=i : x + i # <- this binds i to actual value of iIn your case
lambda i=i : i
would work as "expected".