[Python-Dev] listcomps vs. for loops

Aahz aahz at pythoncraft.com
Wed Oct 22 09:49:13 EDT 2003

On Tue, Oct 21, 2003, Guido van Rossum wrote:
> If you're talking about making
>   x = None
>   for x in R: pass
>   print x # last item of R
> illegal, forget it.  That's too darn useful.

Not illegal, but perhaps for 3.0 we should consider making that print
display "None".  The question is to what extent Python should continue
having unified semantics across constructs.  While I agree that listcomps
should definitely have a local scope ("expressions should not have
side-effects"), I think that there would be advantages to the control
variable in a for loop also having local scope that are magnified by
having compatible semantics between listcomps and for loops.  In other
words, consider

    x = None
    [x for x in R]
    print x

Why should the two behave differently?
Aahz (aahz at pythoncraft.com)           <*>         http://www.pythoncraft.com/

"It is easier to optimize correct code than to correct optimized code."
--Bill Harlan

More information about the Python-Dev mailing list