Python code written in 1998, how to improve/change it?

Carl Cerecke cdc at maxnet.co.nz
Thu Jan 19 22:30:52 EST 2006


Chris Mellon wrote:

> I'm not sure why nobody else in this thread said it, but the most
> common way of implementing state machines I've seen in Python (unless
> theres only a couple states you can manage with if/elif) is to use a
> dict to map states to callables.

Ah. Well, my post suggested, as one option, the callables call
each other directly. No mapping required. But if you want a dict
in there somewhere, feel free to put one in :-)

I was complaining about the function-call overhead of this method
though, which can be quite significant for a FSM where each state
does only a little processing, and there are many transitions -
such as in a parser reading in a large file.

The byte-code rewriting hack mentioned in a previous post
eliminates this overhead. But, I concede, is somewhat ugly.

Cheers,
Carl.



More information about the Python-list mailing list