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

Chris Angelico rosuav at gmail.com
Fri Nov 21 17:47:02 CET 2014


On Sat, Nov 22, 2014 at 3:37 AM, Donald Stufft <donald at stufft.io> wrote:
> I don’t have an opinion on whether it’s enough of a big deal to actually change
> it, but I do find wrapping it with a try: except block and returning easier
> to understand. If you showed me the current code unless I really thought about
> it I wouldn't think about the fact that the next() calls can cause the
> generator to terminate.

And don't forget, by the way, that you can always request the current
behaviour by explicitly wrapping the body of the function in
try/except:

def izip(iterable1, iterable2):
    try:
        it1 = iter(iterable1)
        it2 = iter(iterable2)
        while True:
            v1 = next(it1)
            v2 = next(it2)
            yield v1, v2
    except StopIteration:
        pass

That's exactly what current behaviour does, and if you think that that
try block is too broad and should be narrowed, then you should support
this proposal :)

ChrisA


More information about the Python-Dev mailing list