3 ms·
Ordinary Python function have the same behaviour due to Python has lexical scopes but dynamic namespaces. >>> def add(n): ... return i + n ...
by d0mine 18y ago
Ordinary Python function have the same behaviour due to Python has lexical scopes but dynamic namespaces.
>>> def add(n):
... return i + n
...
>>> fs = [add for i in range(10)]
>>> fs[3](4)
13
This example works due to list-comprehension leaks `i` binding.
http://stackoverflow.com/questions/139819/why-results-of-map-and-list-comprehension-are-different http://stackoverflow.com/questions/139819/why-results-of-map...