4 ms·
for-ins leak iteration variables into the function scope, which can introduce subtle bugs if you're not careful. set/dict/generator comprehensions do not. Exa
by spamizbad 11y ago
for-ins leak iteration variables into the function scope, which can introduce subtle bugs if you're not careful.
set/dict/generator comprehensions do not.
Example:
for foo in bar:
baz(foo)
print foo # Will actually print something
Whereas
whatever = {baz(foo) for foo in bar}
print foo # NameError: name 'foo' is not defined
does not put foo into the outer scope.
list comprehensions in python 2 (but not 3) will leak variables however.