I am trying to generate several functions with different parameter i (see below) using a for loop, but it seems that all these functions are using the last item of i's. Could anyone tell me how to handle this?
This is a simplified example. I actually need to generate more than 200 functions with different parameters.
funs = ()
for i in range(2):
f = lambda x: x+i
funs += (f,)
Then it turns out that the two functions do the same thing:
funs[0](1)
Output: 2
funs[1](1)
Output: 2
But I expect the first function to give a result of 1 rather than 2.
Thank you very much in advance.