[Python-3000] end scope of iteration variables after loop

Robin Stocker robin at nibor.org
Fri Apr 18 18:03:10 CEST 2008


Richard Thomas schrieb:
> I like that loop variables end up still in scope, as demonstrated so
> far on this list it is quite useful, but only when there is a break
> somewhere. The one that confuses me, therefore, is the dummy variables
> in a generator expression leaking into the scope defining that
> expression. Hence:
> 
> x = 0
> L = [f(x) for x in range(2)]
> assert x == 1
> 
> This is not particularly intuitive as the for loop in a generator
> expression can never break; generator expressions feel more "closed".

It's fixed in Python 3:

Python 3.0a4+ (py3k:62372, Apr 18 2008, 17:45:09)
[GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
 >>> x = 0
 >>> l = [str(x) for x in range(2)]
 >>> x
0


Robin


More information about the Python-3000 mailing list