This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: List comprehensions leaking control variable name deprecated
Type: Stage:
Components: Documentation Versions: Python 2.3
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: akuchling Nosy List: akuchling, aleax, sjoerd, skip.montanaro, tcdelaney, terry.reedy
Priority: normal Keywords:

Created on 2003-10-20 23:41 by tcdelaney, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
ref5.diff aleax, 2003-11-02 17:28
Messages (7)
msg18697 - (view) Author: Tim Delaney (tcdelaney) Date: 2003-10-20 23:41
According to the email thread starting:

http://mail.python.org/pipermail/python-dev/2003-
October/039081.html

the control variable name in a list comprehension should 
not be leaked and any use of such a leaked name is 
deprecated.
msg18698 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2003-10-21 00:04
Logged In: YES 
user_id=593130

Ref Man 5.2.4 List displays:  suggested addition at end.

Using leaked 'for <exp_list>' control variables outside the list 
display is deprecated and will not work when the bug is fixed.
msg18699 - (view) Author: Skip Montanaro (skip.montanaro) * (Python triager) Date: 2003-10-21 13:52
Logged In: YES 
user_id=44345

A good point raised by Michael Hudson is that

   foo = [x for x in R]
   print x

will fail if R is empty (and x wasn't previously defined).
All the more reason to deprecate its usage and 
suppress the leaking variable.
msg18700 - (view) Author: Sjoerd Mullender (sjoerd) * (Python committer) Date: 2003-10-21 14:26
Logged In: YES 
user_id=43607

Actually, this is not different from
$ python
Python 2.4a0 (#2, Oct 14 2003, 11:28:48)
[GCC 3.2.2 20030222 (Red Hat Linux 3.2.2-5)] on linux2
Type "help", "copyright", "credits" or "license" for more
information.
>>> for x in []:
...     pass
...
>>> print x
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
NameError: name 'x' is not defined
>>>

where x is also undefined after iteraring through an empty list.
msg18701 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2003-10-21 18:19
Logged In: YES 
user_id=593130

The tutorial will also need revision of deletion:
5.1.4 List Comprehensions
has this:

To make list comprehensions match the behavior of for loops, 
assignments to the loop variable remain visible outside of the 
comprehension: 


>>> x = 100                     # this gets overwritten
>>> [x**3 for x in range(5)]
[0, 1, 8, 27, 64]
>>> x                           # the final value for range(5)
4

msg18702 - (view) Author: Alex Martelli (aleax) * (Python committer) Date: 2003-11-02 17:28
Logged In: YES 
user_id=60314

I notice that the tutorial currently in CVS seems to have been already fixed (by exciding the paragraph tjreedy quoted).  To fix the reference manual, I propose the enclosed patch.
msg18703 - (view) Author: A.M. Kuchling (akuchling) * (Python committer) Date: 2004-08-07 19:16
Logged In: YES 
user_id=11375

I've applied Alex's suggested patch.  Thanks, everyone.
History
Date User Action Args
2022-04-11 14:56:00adminsetgithub: 39434
2003-10-20 23:41:28tcdelaneycreate