PEP 255: Simple Generators

Tim Peters tim at digicool.com
Thu Jun 21 11:16:29 EDT 2001


[Skip Montanaro]
> 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.

I considered it too close to the "maintain a state machine in global
variables" way to be worth splitting out as a distinct case:  the burden of
maintaining a state machine by hand just shifts from mucking with globals to
mucking with instance vrbls.  The latter is preferable, but the former was
easier to explain <wink>.





More information about the Python-list mailing list