Help with coroutine-based state machines?
David Mertz
mertz at gnosis.cx
Thu May 29 16:54:37 EDT 2003
Alan Kennedy <alanmk at hotmail.com> wrote previously:
|yield 'newstate'
|This would be then looked up in a "jump table", i.e. a dictionary
|mapping the state names to generator-iterators representing the
|states, like so
|while 1:
| newstate = jumptable[newstate].next()
|This seemed to me superfluous, since I had already named all of the
|states, through the names of the generator-functions themselves
In retrospect, I agree with Kennedy. In my book, I have revised the
state machine examples to use the state objects directly, rather than
look up their names in a jump table.
That said, the reason I had once done it the way I did was because I had
an example that let users select a jump target, e.g. by typing its name
at a prompt (or a GUI widget with state items would work, although I
never presented that). Users enter strings not object identities. And
likewise, strings (names of states) might come out of text processing,
e.g. reading a file that contains flow instructions.
More information about the Python-list
mailing list