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 = [
i
is local to the list comprehension, but it's available to the lambda because the lambda is in its scope.xrange(10)
. You could do this with lambdas (see the other answer), but you wouldn't want to. Lambdas should be used pretty sparingly.The reason that the lambda behaves this way is because for each loop i
is rebound. Since i isn't local to the lambda, it changes too, and the last value it holds is 9. So all you're doing is 0 + 9
10 times.