[Python-ideas] "While" suggestion

Terry Reedy tjreedy at udel.edu
Fri Aug 22 20:41:21 CEST 2008

Boris Borcic wrote:
> Terry Reedy wrote:

>> So it breaks the intended identity in Py3: list(genexp) == [genexp].
> FYI, there is nothing specific to Py3, Python 2.5 and 2.4 behave the same.

FYI, there *is* something specific to Py3: the willingness to break old 
code.  Originally, list comps were defined as having the same result as 
an equivalent series of for loops and conditional statements:

x = [f(i) for i in seq if g(i)] # same as

_ = []
for i in seq:
   if g(i):
x = _

About the time 2.5 came out (or maybe before), and generator expressions 
were in, it was decided that this definition, which results in 'leaking' 
iteration variables out of comprehensions, was a design mistake, and 
that the new identify given above would be better.  When implementing 
[genexp] as list(genexp) was found to take much longer (2x?), an 
alternative was found that was nearly as fast as the 2.x implementation 
but stopped the leakage and seemed otherwise identical in effect to 
list(genexp).  The only exception I have seen discussed arises from the 
use of StopIteration outside an iterator .__next__ metrhod.


More information about the Python-ideas mailing list