I am getting the following results in Python3
>>> [f() for f in ((lambda: k) for k in range(5))]
[0, 1, 2, 3, 4]
This is what I would expect, however I when I use list comprehension instead, I get:
>>> [f() for f in [(lambda: k) for k in range(5)]]
[4, 4, 4, 4, 4]
Why is that the case? It's very unintuitive to me. I thought that perhaps the function gets somehow copied, but no:
>>> fs = [(lambda: k) for k in range(5)]
>>> fs[0] is fs[1]
False