PEP 255: Simple Generators

Skip Montanaro skip at pobox.com
Thu Jun 21 08:22:10 EDT 2001


    Greg> "Barry A. Warsaw" wrote:
    >> They are very different because the body of a generator function
    >> defines the generator object's .next() method, but the def names the
    >> generator object's factory function.

    Greg> That's an interesting point -- there are actually two separate
    Greg> things going on, but the syntax is collapsing them into one
    Greg> construct.

Sounds to me like the obvious analogue is a class definition with a magic
method named __next__.  When you instantiate the class, __init__ gets called
to set up the computation and the caller receives an instance object for its
trouble.  The __next__ method is then called repeatedly to iterate over the
set of possible values, raising StopIter if/when the sequence is exhausted.

Seems like a missing alternative way to write tokeneater in the PEP.

-- 
Skip Montanaro (skip at pobox.com)
(847)971-7098




More information about the Python-list mailing list