On Thu, Mar 12, 2020 at 10:43 AM Andrew Barnert via Python-ideas <python-ideas@python.org> wrote:
What if a for loop, instead of nexting the iterator and binding the result to the loop variable, instead unbound the loop variable, nexted the Iterator, and bound the result to the loop variable? Presumably this would slow down all loops a very tiny bit (by the cost of checking whether a name is bound) but speed up some—and it would speed up this one even more than your explicit del could (because it doesn’t need an whole extra opcode to loop over, if nothing else). Maybe there’s a way a clever optimization in the compiler and/or interpreter could figure out when it is and isn’t worth doing?

For a "statement" for-loop this would change the semantics, since (even though not everyone likes this behavior) Python currently requires that the loop control variable retain the last value assigned to it. But in comprehensions it might work, since the comprehension control variable lives in an inner scope (at least if it's a simple variable).

--Guido van Rossum (python.org/~guido)