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 comprehension scope
Type: Stage:
Components: Interpreter Core Versions: Python 2.4
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: cito, josiahcarlson, rhettinger, sdahlbac, stutzbach
Priority: normal Keywords:

Created on 2005-01-27 13:27 by sdahlbac, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (8)
msg24070 - (view) Author: Simon Dahlbacka (sdahlbac) Date: 2005-01-27 13:27
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
msg24071 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2005-01-27 13:48
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.
msg24072 - (view) Author: Simon Dahlbacka (sdahlbac) Date: 2005-01-27 14:11
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.
msg24073 - (view) Author: Josiah Carlson (josiahcarlson) * (Python triager) Date: 2005-01-29 19:54
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.
msg24074 - (view) Author: Christoph Zwerschke (cito) * Date: 2006-04-30 11:52
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.
msg62667 - (view) Author: Daniel Stutzbach (stutzbach) (Python committer) Date: 2008-02-22 00:39
The resolution on this bug reads, "In future versions of Python, Guido
would like to change the design to hide the induction variable.  So,
someday, you'll get your wish."

Can this bug be re-opened and the wart removed in 3.0?
msg62669 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2008-02-22 00:47
It's already been implemented for Py3.0.
msg62670 - (view) Author: Daniel Stutzbach (stutzbach) (Python committer) Date: 2008-02-22 00:51
Wonderful!
History
Date User Action Args
2022-04-11 14:56:09adminsetgithub: 41498
2008-02-22 00:51:39stutzbachsetmessages: + msg62670
2008-02-22 00:47:23rhettingersetmessages: + msg62669
2008-02-22 00:39:21stutzbachsetnosy: + stutzbach
messages: + msg62667
2005-01-27 13:27:22sdahlbaccreate