Possible Duplicate:
What do (lambda) function closures capture in Python?
lambda function don't closure the parameter in Python?
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 fn a good (pythonic) rewrite of the code to make it work?