Notice this, you can create functions on runtime, more or less like lambdas
in c++. So basically you are iterating over a list, making n
take values 1,2 and 3
for n in [1, 2, 3]:
def func(x):
return n*x
so, by each iteration you are building a function named func, with takes a value and multiplies it for n. By appending it to the functions list you will have this functions stored, so you can iterate over the list to call the functions.
[function(2) for function in functions]
By doing this you call each of the functions stored with the value 2
, you would expect this to output [2, 4, 6]
([1*2, 2*2, 3*2]), but instead it returns [6, 6, 6]
, WHY?, thats becouse every function use n
for its computation, so they are not really doing 1*x, 2*x and 3*x
but actually n*x
and since n
is bonded in last time to 3
all functions are doing 3*2
wich becomes 6
.
Play around with the python console to check it properly.