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

Paul Moore p.f.moore at gmail.com
Fri Nov 21 15:32:19 CET 2014


On 21 November 2014 13:53, Chris Angelico <rosuav at gmail.com> wrote:
> On Sat, Nov 22, 2014 at 12:47 AM, Raymond Hettinger
> <raymond.hettinger at gmail.com> wrote:
>> Also, the proposal breaks a reasonably useful pattern of calling
>> next(subiterator) inside a generator and letting the generator terminate
>> when the data stream  ends.  Here is an example that I have taught for
>> years:
>>
>>     def izip(iterable1, iterable2):
>>         it1 = iter(iterable1)
>>         it2 = iter(iterable2)
>>         while True:
>>             v1 = next(it1)
>>             v2 = next(it2)
>>             yield v1, v2
>
> Is it obvious to every user that this will consume an element from
> it1, then silently terminate if it2 no longer has any content?

It is to me, certainly.

I'm mostly with Raymond on this. The main exception is that there does
seem to be a trend towards more "tricky" uses of this feature,
typically around ideas such as a stop() function to break out of
generator expressions. I do think this is bad practice and shouldn't
be allowed.

But given that it's possible to write bad code in plenty of ways, and
changing the language "to protect people from themselves" is not a
good idea, I don't think the benefits justify the change [1].

Paul

[1] Probably simply telling people not to try to cram over-complex
code into a genexp would address the worst aspects of the problem...


More information about the Python-Dev mailing list