Incorrect scope of list comprehension variables

Stephen Hansen apt.shansen at gmail.invalid
Sun Apr 4 09:17:43 CEST 2010

On 2010-04-03 23:30:32 -0700, Steve Howell said:

> On Apr 3, 9:58 pm, Tim Roberts <t... at> wrote:
>> Alain Ketterlin <al... at> wrote:
>>> I've just spent a few hours debugging code similar to this:
>>> d = dict()
>>> for r in [1,2,3]:
>>>    d[r] = [r for r in [4,5,6]]
>>> print d
>> 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?

Where exactly does this common sense come from? A list comprehension is 
basically syntactic sugar over a for loop, and...

Python 3.1.2 (r312:79360M, Mar 24 2010, 01:33:18) 
[GCC 4.0.1 (Apple Inc. build 5493)] on darwin
Type "copyright", "credits" or "license()" for more information.
>>> for x in range(10):

>>> x


... p.s: change the ".invalid" to ".com" in email address to reply privately.

More information about the Python-list mailing list