This was exactly what I was thinking of when I said
This is unavoidable in a for loop without breaking existing code, but I think could (and
should?) be changed in a list comprehension
Unfortunately, I was wrong in the last half of this statement.
since the comprehension control variable
lives in an inner scope
The inner scope can still be closed over via a lambda, such as in the following. I think that makes changing the semantics of either a breaking change.
>>> funcs = [lambda: i for i in range(2)] >>> [f() for f in funcs()] [1, 1] # often not intended, but demonstrates the last-value-assigned semantics
Eric