[Python-ideas] Generators are iterators

Nathaniel Smith njs at pobox.com
Wed Dec 10 18:37:24 CET 2014

On 10 Dec 2014 16:38, "Chris Angelico" <rosuav at gmail.com> wrote:
> 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.
> """

I find this more confusing than the original, actually, because now it
sounds like you're saying that the distinction between a generator function
and a generator instance is something new that this PEP is adding, in order
to fix all the problems that are being caused by people writing 'for x in
genfunc: ...'. Which doesn't make much sense. Like Nick said, it seems to
me that the key distinction to emphasize is the distinction between
generator function bodies and __next__ method bodies.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20141210/ea195197/attachment.html>

More information about the Python-ideas mailing list