Note that you can create functions at runtime that are more or less similar to lambdas in C ++. So basically you do iteration over the list, making n take values 1,2 and 3
for n in [1, 2, 3]: def func(x): return n*x
so each iteration you create a function called func that takes a value and multiplies it by n. By adding it to the list of functions, you save these functions so that you can iterate over the list for calling functions.
[function(2) for function in functions]
By doing this, you call each of the functions stored with a value of 2 , you expect this to output [2, 4, 6] ([1 * 2, 2 * 2, 3 * 2]), but instead returns [6, 6, 6] , WHY?, Because each function uses n for its calculations, so they really do not do 1*x, 2*x and 3*x , but actually n*x , and since x is bound for the last time to 3 , all functions perform 3*2 which becomes 6 .
Play around with the python console to check it correctly.
source share