[Python-ideas] Generators are iterators

Steven D'Aprano steve at pearwood.info
Sun Dec 14 02:17:35 CET 2014

On Sat, Dec 13, 2014 at 11:37:23AM -0800, Andrew Barnert wrote:
> 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/.

I read your reply to Chris last night, and it didn't make sense to me 
then and it still doesn't make sense to me now :-( Hence my request for 
a concrete example.

I am still unclear as to what this mystery unnamed thing is. It's not a 
generator function, because that is called a generator function. It's 
not the thing you get when you call a generator function, because that 
is called a generator or an iterator, depending on context.

Surely it's not the generator type, because that has a name: 
"generator". We always use the same name for the class and its 
instances, e.g. we talk about "int" the class as well as saying that 123 
is an int. The class "int" is not itself an int instance, but we still 
call it "int". And likewise for "generator".

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

Agreed. But giving examples of things which aren't the mystery entity 
doesn't really help explain which mystery entity you are talking about 

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

Sure the thing you are defining when you write "def gen() ..." is a 
function? I thought we were in agreement about that. It's a function 
with a flag set, a.k.a. a generator function.

If you're referring to something else, a concrete example will help. 
Because as we stand now, I don't know whether we're talking about 
something important or not, or if we are in agreement, disagreement, or 
merely confusion :-)


More information about the Python-ideas mailing list