[Python-ideas] Generators are iterators

Chris Angelico rosuav at gmail.com
Wed Dec 10 17:35:04 CET 2014


On Thu, Dec 11, 2014 at 3:21 AM, Nick Coghlan <ncoghlan at gmail.com> wrote:
> On 11 December 2014 at 00:18, Steven D'Aprano <steve at pearwood.info> wrote:
>>
>> If generators are to cease to be iterators, what will they be?
>
> What the PEP is trying to say is that generators are not the same
> thing as __next__ method implementations (and I agree that shortening
> the latter to "iterators" is incorrect).

Current wording: (at least, I think this is the only bit that's questionable)

"""
Under this proposal, generators and iterators would be distinct, but
related, concepts.  Like the mixing of text and bytes in Python 2,
the mixing of generators and iterators has resulted in certain
perceived conveniences, but proper separation will make bugs more
visible.
"""

Would it be better to clarify that as "generator functions"? Maybe
something like this:

"""
Under this proposal, generator functions and iterators would be
distinct, but related, concepts.  Like the mixing of text and bytes in
Python 2, the mixing of generators and iterators has resulted in
certain perceived conveniences, but proper separation will make bugs
more visible. The distinction is simple: A generator function returns
a generator object. The latter is an iterator, having proper __iter__
and __next__ methods, while the former has neither and does not follow
iterator protocol.
"""

(Most of the text ignores __iter__, even though technically it's an
important part of iterator protocol for it to exist and return self. I
don't think anyone will be confused by the omission, as it perfectly
follows __next__, but I could add text if people think it's
important.)

ChrisA


More information about the Python-ideas mailing list