[ python-Bugs-1110705 ] list comprehension scope

SourceForge.net noreply at sourceforge.net
Sat Jan 29 20:54:25 CET 2005


Bugs item #1110705, was opened at 2005-01-27 05:27
Message generated for change (Comment added) made by josiahcarlson
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1110705&group_id=5470

Category: Python Interpreter Core
Group: Python 2.4
Status: Closed
Resolution: Invalid
Priority: 5
Submitted By: Simon Dahlbacka (sdahlbac)
Assigned to: Nobody/Anonymous (nobody)
Summary: list comprehension scope

Initial Comment:
The variable used for iteration in list comprehensions
is still in scope after the list comprehension. 

>>>foo = [1, 2, 3]
>>>bar = [dummy + 1 for dummy in foo]
>>>dummy
3

Expected result: NameError: name 'dummy' is not defined

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

Comment By: Josiah Carlson (josiahcarlson)
Date: 2005-01-29 11:54

Message:
Logged In: YES 
user_id=341410

"this behavior is deprecated, and relying on it will not
work once this bug is fixed in a future release"

As we have not yet reached the future release in which this
will happen, and the 'bug' is both known and documented, it
will be 'fixed' when it is fixed.  The reason that it has
not yet been 'fixed' is because there is a nontrivial amount
of code that would break if it was.

I personally would call it a deprecated 'feature'.  Then
again, I use it more often than I should.

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

Comment By: Simon Dahlbacka (sdahlbac)
Date: 2005-01-27 06:11

Message:
Logged In: YES 
user_id=750513

Seemingly (according to:
http://docs.python.org/ref/lists.html) this is indeed a bug
that should be fixed.

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

Comment By: Raymond Hettinger (rhettinger)
Date: 2005-01-27 05:48

Message:
Logged In: YES 
user_id=80475

This is not a bug.  It is behaving as designed.  The thought
was to emulate the behavior of an equivalent for-loop.

In future versions of Python, Guido would like to change the
design to hide the induction variable.  So, someday, you'll
get your wish.

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

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


More information about the Python-bugs-list mailing list