[Python-ideas] PEP 479: Change StopIteration handling inside generators
Steven D'Aprano
steve at pearwood.info
Fri Nov 21 18:16:23 CET 2014
On Fri, Nov 21, 2014 at 08:52:59AM -0800, Ethan Furman 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.
"Must not support send()" has never been part of the definition of
iterators.
The `Iterator` ABC also recognises generators as iterators:
py> def gen():
... yield 1
...
py> from collections import Iterator
py> isinstance(gen(), Iterator)
True
and they are documented as iterators:
Python’s generators provide a convenient way to implement
the iterator protocol. If a container object’s __iter__()
method is implemented as a generator, it will automatically
return an iterator object (technically, a generator object)
supplying the __iter__() and __next__() methods.
https://docs.python.org/3/library/stdtypes.html#generator-types
I don't understand where this idea that generators aren't iterators has
come from, unless it is confusion between the generator *function* and
the generator object itself.
--
Steven
More information about the Python-ideas
mailing list