When using lambda expression in for loop and variable i, all lambda expression stored in tmp list are dependent on i.
tmp = []
for i in range(5):
tmp.append(lambda x: x + i)
print(tmp[0](1)) # Prints 5 should print 1
print(tmp[4](1)) # Prints 5 should print 5
Basically, it boils down to this problem, where lambda expression is dependent on a.
a = 10
foo = lambda x: x + a
print(foo(1)) # Prints 11 should print 11
a = 20
print(foo(1)) # Prints 21 should print 11
Is it possible to use var for initialization but afterwards not be dependent afterwards anymore. Solution for the for loop with lambda expression is needed. Thanks
I tried to boild down the problem and also tried a hack with lambda x: x + eval(str(i)) to paste the value and not the local variable.