by the_mitsuhiko · 4y ago · 0 comments
>>> def foo():
... return [lambda: x for x in range(10)]
...
>>> [x() for x in foo()]
[9, 9, 9, 9, 9, 9, 9, 9, 9, 9]A common sentiment when Go was unveiled was that its designers had ignored the 35-odd years of language research and experience since C.
>>> [f() for f in (lambda: x for x in range(10))]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
List comprehensions have been around for longer, and so behave the way they do for backwards compatibility reasons.