[Python-ideas] Generators are iterators

Chris Barker chris.barker at noaa.gov
Wed Dec 10 18:55:44 CET 2014

On Wed, Dec 10, 2014 at 8:35 AM, Chris Angelico <rosuav at gmail.com> wrote:

> > 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).

agreed that this is moistly semantics, so yes, "generator functions" is
probably better.

Also, IIUC, there is no such thing (or at least not one thing) as an
"iterator" -- rather there are various things that conform to the iterator
protocol. The most common of these is a class that implements  __iter__ and
__next__ methods (that behave the way specified) Should we call that an
"iterator class"?

Whereas there IS such a thing as a "generator" -- it is the type that is
returned by a generator function or generator expression (comprehension?)

And generators are one of the types that conform to the iterator protocol

So, and here is where I am a bit confused, does this PEP change somethign
about the generator type, or something about the syntax of for writing
generator functions? (or both?)

If it's really changing the behavior of the type, then the PEP should use

> 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.

I think this confuses things, too: generator functions and iterators have
always been completely different -- again, there is no such thing as an
iterator at all, there are only things that conform to the protocol, and as
I think Oscar pointed out, the behavior of StopIteration _inside_ such a
type is not specified by the protocol.

We could define an "iterator" as anything that conforms to the protocol --
probably how it is being used for the most part already. In which case.

generators are a type of iterator
custom classes with __iter__ and __next__ are another type of iterator
some build-in objects are also iterators -- range objects, files, what have

So how to write the above paragraph? How about something like:

Under this proposal, generator functions and custom iterator classes would
more distinct than they currently are. They are two ways of writing an
iterator, but with not only different syntax but different behavior with
respect to the handling of StopIteration within the object itself. But
generator functions have always been a compact way to write an iterator
that "does some of the book keeping for you" -- with this PEP, they will do
a bit more of the book keeping.

Like the mixing of text and bytes in Python 2, the mixing of generators and
has resulted in certain perceived conveniences,

I don't know that generators and iterators have ever been "mixed" in the
way that text and bytes were. So I think this analogy just confused things.

The distinction is simple: A generator function returns 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'm not sure there is confusion about that either -- at least not the
confusion that is being addressed here.



Christopher Barker, Ph.D.

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris.Barker at noaa.gov
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20141210/f6a0b11a/attachment-0001.html>

More information about the Python-ideas mailing list