I have encountered a problem in Python that I'd be happy if someone could help! I'm trying to execute the following code block:
def func(x,y):
return x+y
if __name__=='__main__':
func_list=[]
for y in [1,2]:
func_list.append(lambda x: func(x,y))
print(func_list[0](0))
print(func_list[1](0))
the output is
2
2
while what I expect is
1
2
I know (to some extent) that this happens because Python merges references when two variables are assigned equal. My question is, how to correct this code to obtain two lambda functions such that they behave expectedly as above?