Use of iterators for state machine (was Re: 2.2 features)

Tim Peters tim.one at home.com
Thu Jul 26 08:33:32 CEST 2001


[Peter Hansen]
> I like the potential for iterators for infinite sequences of several
> kinds (where the ability to "get out of a loop early" is an absolute
> requirement :-).
>
> I'm especially looking forward to using them to implement state machines.
>
> I suspect they will greatly simplify the readability of several
> different state machine patterns I tend to use.  Has anyone with more
> of a background in the whole iterator concept (in other languages
> of course) tried this before?  Any comments?

Have you read PEP 255 ("Simple Generators")?  There's a long section at the
start discussing exactly that.  Generators are great for programming state
machines, for reasons explained there in some detail (briefly that state can
often be left implicit in control flow and local bindings rather than
explicitly materialized into masses of stacks and persistent vrbls).

They're not as powerful as full-blown coroutines (a 255-flavor generator can
only suspend to its immediate invoker, and that's what's "Simple" about
them), but you may be surprised at how far you can get despite that
limitation.

What you can't do is, in a generator G, yield directly back to G's invoker
from a routine called *by* G.  But if the routine called by G is also a
generator, it can yield whatever it likes whenever it likes back to G, and G
can in turn yield that back to its invoker.  Both routines are suspended
then, and resuming G will as a matter of course resume the generator it
invoked too.

If you don't think about it too much <wink>, it all works the way you
expect; if you do think about it too much, it takes an "aha!" (or maybe even
two) to figure out *why* it all works.  It took me about 8 years to explain
it to Guido <wink> -- although, in his defense, he may wish to protest that
the first time he actually paid attention to what I was screaming at him, he
"got it" in about one minute flat.

pretty-good-for-a-malevolent-dutch-guy-ly y'rs  - tim





More information about the Python-list mailing list