Incorrect scope of list comprehension variables

Steven D'Aprano steve at
Mon Apr 5 02:01:20 CEST 2010

On Sun, 04 Apr 2010 05:50:01 -0700, Ethan Furman wrote:

>>>Yes, this has been fixed in later revisions, but I'm curious to know
>>>what led you to believe that a list comprehension created a new scope. 
>>>I don't that was ever promised.
>> Common sense about how programming languages should work?  As confirmed
>> by later revisions?
> Common sense?  About *somebody else's* idea of how a programming
> language should work?

Nevertheless, it is a common intuition that the list comp variable should 
*not* be exposed outside of the list comp, and that the for-loop variable 
should. Perhaps it makes no sense, but it is very common -- I've never 
heard of anyone being surprised that the for-loop variable is exposed, 
but I've seen many people surprised by the fact that list-comps do expose 
their loop variable.


More information about the Python-list mailing list