[Python-Dev] Suggested amendment to PEP 255

Jeremy Hylton jeremy@alum.mit.edu
Wed, 20 Jun 2001 19:47:28 -0400

> My vote is for a "generator" keyword to introduce the code block of a
> generator.  Makes perfect sense to me, and it will be a strong
> indication to anybody reading my code that something special is going
> on.  And something special /is/ going on!
> An informal poll of PythonLabs indicates a split on this subject,
> perhaps setting Jeremy up as a Sandra Day O'Conner swing vote.  But
> who said this was a democracy anyway? :)
> somewhat-like-my-own-country-of-origin-ly y'rs,
> -Barry

That's a nice analogy, Ruth Barry Ginsburg; a Supreme Court, which appoints
the president, seems a closer fit to Python's dictatorship than some sort of
democratic process.  I wasn't present for the oral arguments, but I'm sure
we all know how Tim Scalia voted and that Guido van Clarence Thomas agreed
without comment.  I assume, then, that Anthony Kennedy Jr. joined you,
although he's often a swing vote, too.  Can't wait to hear the report from
Nina "Michael Hudson" Totenberg.

I was originally happy with the use of def.  It's not much of a stretch
since the def statement defines a code block that has formal parameters and
creates a new scope.  I certainly wouldn't be upset if Python ended up using
def to define a generator.

I appreciate, though, that the definition of a generator may look an awful
lot like a function.  I can imagine a user reading a module, missing the
yield statement, and trying to use the generator as a function.  I can't
imagine this would happen often.  My limited experience with CLU suggests
that iterators aren't going to be huge, unwieldy blocks where it's hard to
see what the ultimate control flow is.  If a confused user treats a
generator as a regular function, he or she certainly can't expect it to
return anything useful, since all the return statements are bare returns;
the expected behavior would be some side-effect on global state, which seems
both unlikely and unseemly for an iterator.

I'm not sure how hard it will be to explain generators to new users.  I
expect you would teach functions and iterations via for loop, then explain
that there is a special kind of function called a generator that can be used
in a for loop.  It uses a yield statement instead of a return statement to
return values.  Not all that hard.  If we use a different keyword to
introduce them, you'd probably explain them much the same way: A generator
is a special kind of function that can be used in a for loop and is defined
with generator instead of def.

As other people have mentioned, Icon doesn't use special syntax to introduce
generators.  We might as well look at CLU, too, where a different approach.
You can view the CLU Reference Manual at:

It uses "proc" to introduce a procedure and "iter" to introduce an iterator.
See page 72 for the details:


It's a toss up, then between the historical antecedents Icon and CLU.  I'd
tend to favor a new keyword for generators, but could be talked out of that