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)]
Nice catch. The lambda in the list comprehension is seeing the same local i every time.
i
You can rewrite it as:
a = [] for i in range(10): a.append(makefun(i)) b = [] for i in range(10): b.append(lambda: i)
with the same result.