Generating functions inside loop with lambda expression in python

Viewed 9442

If I make two lists of functions:

def makeFun(i):
    return lambda: i

a = [makeFun(i) for i in range(10)]
b = [lambda: i for i in range(10)]

why do lists a and b not behave in the save way?

For example:

>>> a[2]()
2
>>> b[2]()
9
6 Answers
Related