I have created below code which uses lambda inside a for loop:
adders = []
for n in range(1, 4):
adders.append(lambda x: x + n)
I am getting below output for adders[0](10)
13
My understanding is that x + n
will be evaluated at the compile time.
So, the output for adders[0](10)
should be :
11
My understanding is that python should be adding lambda x: x + 1
, lambda x: x + 2
and lambda x: x + 3
to adders
. Please help me understand why python is not doing that?
Please let me know if I am misunderstanding something.