[Python-ideas] PEP 479: Change StopIteration handling inside generators

Ethan Furman ethan at stoneleaf.us
Wed Nov 26 00:46:00 CET 2014


On 11/25/2014 03:31 PM, Chris Angelico wrote:
> On Wed, Nov 26, 2014 at 8:56 AM,  <random832 at fastmail.us> wrote:
>> For something more concrete, we can consider a naive implementation of
>> iteration over adjacent pairs:
>>
>> def pairs(x):
>>     i = iter(x)
>>     while True:
>>         yield next(i), next(i)
> 
> Okay, it's simple and naive. How about this version:
> 
> def pairs(x):
>     i = iter(x)
>     for val in i:
>         yield val, next(i)
> 
> Also simple, but subtly different from your version. What's the
> difference? Will it be obvious to everyone who reads it?

I don't see the difference being subtle enough -- if an odd number of items is tossed in, that `next(i)` is still going
to raise a StopIteration, which under PEP 479 will become a RunTimeError.

Or did you mean that even numbered iterators will work fine, but odd-numbered ones will still raise?  Nice.  :)

--
~Ethan~

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: OpenPGP digital signature
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20141125/d24d03f5/attachment.sig>


More information about the Python-ideas mailing list