A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://mail.python.org/pipermail/python-dev/2003-October/039287.html below:

[Python-Dev] listcomps vs. for loops

[Python-Dev] listcomps vs. for loopsGuido van Rossum guido at python.org
Wed Oct 22 11:06:43 EDT 2003
> > 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?

The variable of a for *statement* must be accessible after the loop
because you might want to break out of the loop with a specific
value.  This is a common pattern that I have no intent of breaking.
So it can't introduce a new scope; then it might as well keep the last
value assigned to it.

List comprehensions and generator expressions don't have 'break'.
(You could cause an exception and catch it, but it's not a common
pattern to use the control variable afterwards -- only the debugger
would need access somehow.)

--Guido van Rossum (home page: http://www.python.org/~guido/)

More information about the Python-Dev mailing list

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4