I found a interesting behavior of lambda function in Python.
If I do,
lambda_funcs = []
for i in range(10):
lambda_func = lambda x:x[i]
lambda_funcs.append(lambda_func)
X = ['a', 'b', 'c']
print lambda_funcs[0](X)
It throws an error: IndexError: list index out of range.
This code aims to create 10 lambda functions each of which returns i-th element. But, all lambda_func in the above code attempts to retrieve 10th element.
To make it work, I found that the following definition is a right way:
lambda_func = (lambda j: lambda x: x[j])(i)
Can anyone tell me what is going on inside lambda evaluation in Python?