I am new in Python. My task was quite simple — I need a list of functions that I can use to do things in batch. So I toyed it with some examples like
fs = [lambda x: x + i for i in xrange(10)]
Surprisingly, the call of
[f(0) for f in fs]
gave me the result like [9, 9, 9, 9, 9, 9, 9, 9, 9, 9]
. It was not what I expected as I'd like the variable i
has different values in different functions.
So My question is:
-
Is the variable
i
in lambda global or local? -
Does python has the same concept like 'closure' in javascript? I mean does each lambda here holds a reference to the
i
variable or they just hold a copy of the value ofi
in each? -
What should I do if I'd like the output to be
[0, 1, .....9]
in this case?
Best Answer
It looks a bit messy, but you can get what you want by doing something like this:
Normally Python supports the "closure" concept similar to what you're used to in Javascript. However, for this particular case of a lambda expression inside a list comprehension, it seems as though
i
is only bound once and takes on each value in succession, leaving each returned function to act as thoughi
is 9. The above hack explicitly passes each value ofi
into a lambda that returns another lambda, using the captured value ofy
.