[ python-Bugs-1110705 ] list comprehension scope

SourceForge.net noreply at sourceforge.net
Sun Apr 30 13:52:49 CEST 2006


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

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
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: Christoph Zwerschke (cito)
Date: 2006-04-30 13:52

Message:
Logged In: YES 
user_id=193957

Either this is a bug that should be fixed, or the
documentation that says it is a bug should be fixed
(http://www.python.org/doc/current/ref/lists.html#foot4519).

That fact that an ordinary for loop leaks the control
variables into the containing scope may be a design feature,
but the same behavior for list comprehensions can hardly be
called a design feature. At least not one that is
reasonable. I would rather call it a bug. It definitely
contradicts the principle of least surprise.

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

Comment By: Josiah Carlson (josiahcarlson)
Date: 2005-01-29 20: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 15: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 14: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