[Python-ideas] Generators are iterators

Andrew Barnert abarnert at yahoo.com
Sat Dec 13 20:37:23 CET 2014


On Dec 13, 2014, at 5:36, Steven D'Aprano <steve at pearwood.info> wrote:

> On Fri, Dec 12, 2014 at 03:22:32PM -0800, Andrew Barnert wrote:
> 
>> In Python, the term "Iterator" is just as consistent and meaningful as 
>> in all these other languages. The fact that some people confuse 
>> iterables and iterators isn't a reason to abandon this simplicity.
> [...]
> 
> +1 to this.
> 
> 
> But:
> 
>> The things we need to be clear about here are the things that _dont't_ 
>> have an official name. In particular, the thing that's built from 
>> calling a generator function or evaluating a generator expression and 
>> used by the generator.__next__ method is not a generator, a generator 
>> function, a generator function body, an iterator, or anything else 
>> with a name in the language. And that's what's confusing people.
> 
> I don't know what thing you are referring to. If I write this:
> 
> py> def gen():
> ...     yield 1
> ...
> py> it = gen()
> 
> then `gen` is a function.

Chris Barker just asked exactly the same question yesterday, except that he trimmed the quote more and used "g" instead of "it" for his example. So I'll just paste the same reply, and you can manually s/g/it/.

Of course g is an instance of generator (and therefore also an iterator).

But you're not defining the generator type here--that already exists as a builtin type. What you're defining is a thing made up of a code object and a frame, which is used by that generator instance to do its generating. That thing is not a generator, or a generator type (or an iterator instance or type), or a __next__ method, or anything else with a name.

(I don't know if that's the thing Guido was trying to describe as not an iterator, but I don't think that's too important, since he's already acknowledged that the distinction he was trying to make isn't important anyway.)


> It's a specific kind of function that uses 
> "yield", and the name for that is a generator function. Informally, 
> sometimes people call it a "generator", which is a bad habit due to the 
> possibility of confusing `gen` with `it`, but since it is quite rare to 
> be in a position where such confusion can occur (PEP 479 and related 
> discussions not withstanding), we can often get away with such sloppy 
> terminology.
> 
> Also, `it` is an iterator. It's also a generator:
> 
> py> type(it)
> <class 'generator'>
> 
> The same applies to generator expressions:
> 
> py> type(c for c in "aaa")
> <class 'generator'>
> 
> So I'm not sure which mysterious object you are referring to that 
> doesn't have a standard name. Can you give a concrete example?
> 
> 
> 
> -- 
> Steven
> _______________________________________________
> 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/


More information about the Python-ideas mailing list