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

2021-06-07 Thread Mark Dickinson


Change by Mark Dickinson :


--
status: open -> closed

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



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

2021-06-06 Thread Steven D'Aprano


Steven D'Aprano  added the comment:

By the way, loop variables are not considered to be "temporary" in Python. They 
are no more temporary than any other local variable -- they *are* local 
variables with exactly the same scope and lifetime as all other local variables.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



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

2021-06-06 Thread Steven D'Aprano


Steven D'Aprano  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 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



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

2021-06-06 Thread Debasis Satapathy


New submission from Debasis Satapathy :

numbers = [1, 2, 3, 4, 5, 6, 7, 8]
for x in numbers:
print(x)
print(x)

In the above code, print(x) statement should give error. Because x scope should 
be local to for loop only.
99% cases, developers will not use the temporary variable x outside of the for 
loop.
So x will keep on consuming memory always. So it is a bad usage of memory.
Ideally x memory should be free once for loop execution is completed.

--
components: Library (Lib)
messages: 395246
nosy: deb_ctc
priority: normal
severity: normal
status: open
title: For Loop temporary variable scope should be local to For loop
type: behavior
versions: Python 3.9

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com