I am trying to create lambdas inside a loop that iterates over a list of objects:
lambdas_list = []
for obj in obj_list:
lambdas_list.append(lambda : obj.some_var)
Now, if I iterate over the lambdas list and call them like this:
for f in lambdas_list:
print(f())
I get the same value. That is the value of the last obj in obj_list since that was the last variable in the block of the list iterator. Any ideas for a good (pythonic) rewrite of the code to make it work?
Use this line instead: