Basically, in the example below, why do the lambdas in f2
capture a reference to the list comprehension loop variable, but those in f3
"capture" (really, they just accept an argument) "the way we expect" lambda capture to work?
f1 = [lambda x: x, lambda x: x + 1, lambda x: x + 2]
print([f(0) for f in f1])
f2 = [lambda x: f(x) + 1 for f in f1]
print([f(0) for f in f2])
f3 = [(lambda ff: lambda x: ff(x) + 1)(f) for f in f1]
print([f(0) for f in f3])
The three lines outputted are:
[0, 1, 2]
[3, 3, 3]
[1, 2, 3]