[issue44332] For Loop temporary variable scope should be local to For loop

Steven D'Aprano report at bugs.python.org
Mon Jun 7 01:43:32 EDT 2021


Steven D'Aprano <steve+python at pearwood.info> added the comment:

This is not a bug, it is intentional design and has been since Python 1.

You are incorrect about x consuming memory "always". If the value bound to x is in use elsewhere, deleting x will save no memory. If the value is not in use elsewhere, it will be garbage collected as soon as x goes out of scope, which will be at the end of the function.

Shifting to block-scope for for-loops has been discussed before, it is not a popular idea and I expect that most people will oppose it. You can search the Python-Ideas mailing list if you want to find out more. If you still feel strongly that this is an enhancement, as it is a major change in language behaviour it would require a PEP to be written.

https://www.python.org/dev/peps/pep-0001/

Even if the PEP was accepted, the earliest it could go into the language would be Python 3.11 or better, and that would likely require a special `__future__` import.

----------
nosy: +steven.daprano
resolution:  -> not a bug
stage:  -> resolved

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue44332>
_______________________________________


More information about the Python-bugs-list mailing list