[Python-3000] end scope of iteration variables after loop
Richard Thomas
R.W.Thomas.02 at cantab.net
Fri Apr 18 17:29:03 CEST 2008
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".
Richard.
On Fri, Apr 18, 2008 at 2:15 PM, Facundo Batista
<facundobatista at gmail.com> wrote:
> 2008/4/18, Nicholas T <ntung at ntung.com>:
>
>
> > Amaury - I think it's generally cleaner code to write
> > for myObject in someList:
> > if myObject.fits():
> > process(myObject)
> > break
> > than
> > for myObject in someList:
> > if myObject.fits():
> > break
> > process(myObject)
>
> See, I do this a lot:
>
> for a, b, c in someList:
> if <some condition with a or b>:
> break
> else:
> c = foobar
> c.something()
>
> Regards,
>
> --
> . Facundo
>
> Blog: http://www.taniquetil.com.ar/plog/
> PyAr: http://www.python.org/ar/
>
>
> _______________________________________________
> Python-3000 mailing list
> Python-3000 at python.org
> http://mail.python.org/mailman/listinfo/python-3000
> Unsubscribe: http://mail.python.org/mailman/options/python-3000/r.w.thomas.02%40cantab.net
>
More information about the Python-3000
mailing list