[Python-ideas] "Iteration stopping" syntax [Was: Is this PEP-able? for X in ListY while conditionZ:]
Nick Coghlan
ncoghlan at gmail.com
Mon Jul 1 00:28:50 CEST 2013
More information about the Python-ideas mailing list
Mon Jul 1 00:28:50 CEST 2013
- Previous message: [Python-ideas] "Iteration stopping" syntax [Was: Is this PEP-able? for X in ListY while conditionZ:]
- Next message: [Python-ideas] "Iteration stopping" syntax [Was: Is this PEP-able? for X in ListY while conditionZ:]
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On 1 Jul 2013 07:01, "Guido van Rossum" <guido at python.org> wrote: > > I apologize, this thread was too long for me to follow. Is the issue > the following? > > >>> def stopif(x): > ... if x: raise StopIteration > ... return True > ... > >>> [i for i in range(10) if stopif(i==3)] > Traceback (most recent call last): > File "<stdin>", line 1, in <module> > File "<stdin>", line 1, in <listcomp> > File "<stdin>", line 2, in stopif > StopIteration > >>> list(i for i in range(10) if stopif(i==3)) > [0, 1, 2] > > I.e. the difference between list(<genexp>) and [<genexp>] is that if > <genexp> raises StopIteration, list(...) returns the elements up to > that point but [...] passes the exception out? > > That seems a bug to me inherited from the Python 2 implementation of > list comprehensions and I'm fine with fixing it in 3.4. The intention > of the changes to comprehensions in Python 3 was that these two forms > would be completely equivalent. The difficulty has always been that > CPython comprehensions were traditionally faster than generator > expressions and we're reluctant to give that up. But it's still a bug. Yep, and Andrew pointed out the overhead of fixing it is actually quite low - we just have to tweak comprehensions to wrap the entire loop in a try/except that ignores the StopIteration exception. That brings them into line with the generator form where it doesn't matter if the exception comes directly from the generator code or is raised by the interpreter due to the frame terminating, the loop implicit in the list call will treat it as indicating the end of the generator. Cheers, Nick. > > -- > --Guido van Rossum (python.org/~guido) > _______________________________________________ > Python-ideas mailing list > Python-ideas at python.org > http://mail.python.org/mailman/listinfo/python-ideas -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130701/dc556969/attachment.html>
- Previous message: [Python-ideas] "Iteration stopping" syntax [Was: Is this PEP-able? for X in ListY while conditionZ:]
- Next message: [Python-ideas] "Iteration stopping" syntax [Was: Is this PEP-able? for X in ListY while conditionZ:]
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-ideas mailing list