Possible Duplicate:
What’s going on with the lambda expression in this python function?
What do (lambda) function closures capture in Python?
For example, I have a list of string ['a', 'b', 'c'].
I want to generate one dictionary with the element in that list ('a', 'b' and 'c') as key, and a function which prints the key as the value (print('a'), print('b'), print('c'))
from __future__ import print_function
l = ['a', 'b', 'c']
m = {k: lambda : print(k) for k in l}
I wrote the above code. But the result is not right
m['a']()
c
m['b']()
c
I know that if I don't use lambda there
m = {k:print(k) for k in l}
This is no problem. But i'd like to know why lambda does not work here.
Any idea how to solve this? Thanks