[Python-ideas] "Iteration stopping" syntax [Was: Is this PEP-able? for X in ListY while conditionZ:]
Guido van Rossum
guido at python.org
Mon Jul 1 01:24:38 CEST 2013
On Sun, Jun 30, 2013 at 3:28 PM, Nick Coghlan <ncoghlan at gmail.com> wrote:
>
> 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.
Thanks, sounds good.
--
--Guido van Rossum (python.org/~guido)
More information about the Python-ideas
mailing list