[Python-bugs-list] [ python-Bugs-827209 ] List comprehensions leaking control variable name deprecated

SourceForge.net noreply at sourceforge.net
Tue Oct 21 14:19:47 EDT 2003


Bugs item #827209, was opened at 2003-10-20 19:41
Message generated for change (Comment added) made by tjreedy
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=827209&group_id=5470

Category: Documentation
Group: Python 2.3
Status: Open
Resolution: None
Priority: 5
Submitted By: Tim Delaney (tcdelaney)
Assigned to: Nobody/Anonymous (nobody)
Summary: List comprehensions leaking control variable name deprecated

Initial Comment:
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.

----------------------------------------------------------------------

Comment By: Terry J. Reedy (tjreedy)
Date: 2003-10-21 14:19

Message:
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



----------------------------------------------------------------------

Comment By: Sjoerd Mullender (sjoerd)
Date: 2003-10-21 10:26

Message:
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.

----------------------------------------------------------------------

Comment By: Skip Montanaro (montanaro)
Date: 2003-10-21 09:52

Message:
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.


----------------------------------------------------------------------

Comment By: Terry J. Reedy (tjreedy)
Date: 2003-10-20 20:04

Message:
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.


----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=827209&group_id=5470



More information about the Python-bugs-list mailing list