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

Andrew Barnert abarnert at yahoo.com
Fri Nov 21 18:18:29 CET 2014


On Nov 21, 2014, at 8:52, Ethan Furman <ethan at stoneleaf.us> wrote:

> On 11/21/2014 08:30 AM, Steven D'Aprano wrote:
>> 
>> But generators and iterators *are the same thing*. (Generator functions 
>> are not iterators, but generators themselves are.) Iterators don't have 
>> a specific type, but they obey the iterator protocol:
> 
> Um, no, they aren't.  One cannot 'send()' into any ol' iterator.

Generators are a subtype of iterators. They support the iterator protocol completely, and in the same way as any other iterator. They also support extensions to that protocol--e.g., send(). But they also have a relationship to a generator function or generator expression, which you could call a "protocol" but if so it's not one expressible at the level of the language.

I think that leads to a bit of confusion when speaking loosely. When someone says "the generator protocol vs. the iterator protocol" the "obviously correct" meaning is send and throw, but it's not what people always mean.

Then again, the word "generator" itself leads to confusion when speaking loosely. Maybe it would be clearer if "generator" had no meaning; generator functions return generator iterators. But I don't think this confusion has caused serious problems over the decades, so I doubt the more minor confusion at issue here is likely to be serious.



More information about the Python-ideas mailing list