PEP 255: Simple Generators

Rainer Deyke root at
Thu Jun 21 00:27:54 CEST 2001

"David Bolen" <db3l at> wrote in message
news:ur8wevrz0.fsf at
> But isn't a generator really just a function that is returning an
> iterator?  That is, why is it "very" different from a function,
> internal implementation aside, which we agree it's not necessary to
> reveal.

o = f()

To the caller, it doesn't matter if 'f' is a function, a generator, a bound
method, a class, or any other callable object.  That does *not* mean that
functions, generators, classes and other callable objects should have the
same syntax: they are fundamentally different ways of implementing callable
objects.  If generators absolutely have to reuse syntax (which is not the
case), I would prefer the keyword 'class' over 'def', since calling a
generator creates an instance.  Reusing 'class' also increases readability
since it's easier to see the difference between a generator and a class by
looking at the contents.

Rainer Deyke (root at
Shareware computer games           - 
"In ihren Reihen zu stehen heisst unter Feinden zu kaempfen" - Abigor

More information about the Python-list mailing list