[Python-ideas] Generators are iterators

Chris Barker chris.barker at noaa.gov
Fri Dec 12 22:51:39 CET 2014

On Fri, Dec 12, 2014 at 12:14 PM, Alexander Belopolsky <
alexander.belopolsky at gmail.com> wrote:
> This gets you a custom iterable.  The corresponding iterator will be the
>> buitin iterator type:
>> >>> class X:
>> ...   __getitem__ = None
>> ...
>> >>> iter(X())
>> <iterator object at 0x10b2a70b8>
> The same logic applies to generators and I think this misconception is at
> the root of the confusion in this thread.   When you are writing a
> generator function, you are not creating a custom TIIP.  You are writing a
> code object that Python knows how to convert into an instance of a built-in
> TIIP (the generator type).

The point was supposed to be that you want a TIIP with particular behavior
that is not built-in (hence custom). Yes, in terms of type, a generator
function, when called, will return the a built-in type, but its behavior is

Indeed the same as writing a class with __getitem__ and no __iter__ or
__next__ will give you an instance a built-in iterator type with customized

I guess my take is that the user shouldn't have to know exactly what the
type of TIIP is produced is -- only what its behavior will be.

But maybe that IS the source of much of this confusion -- the PEP authors
make the point that generators are something special. I want tend to think
of them as an implementation detail. This PEP makes them more special, and
that's really the point of the contentious part of the PEP.

So maybe that's the the point we are trying to make -- while we can argue
about whether generators ARE iterators, or whether they have been
"conflated" -- generators are special, and might as well be more special ;-)


> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/


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/20141212/5e2ac7d6/attachment.html>

More information about the Python-ideas mailing list