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

Dan Sommers me at
Thu Jan 19 17:42:58 EST 2006

On Fri, 20 Jan 2006 10:27:58 +1300,
Carl Cerecke <cdc at> wrote:

> Petr Jakes wrote:

[ a query regarding some 1998 python code that implements a finite state
    machine ]

> Python has no goto.

Thank goodness!

> func = f_state_1 # start_state

> while func != None:
>    func()

> We've eliminated the numerous comparisons, and it is, arguably, more
> pythonic ...


> ... now you have a function-call overhead on each state transition ...

Have you profiled your code and demonstrated that this particular
function call consumes too much time?

If you can't stand the overhead of one [extra, parameterless] function
call per transition, especially *after* having eliminated the numerous
comparisons, then Python may not be the best tool for the job.

> ... and any information shared between states has to be in an
> enclosing scope.

Well, no, not exactly.  Consider an instance of a class that contains
its own data (and perhaps some of its own transition functions) but
inherits the basic state machine machinery from a finite state machine
class (or simply passes itself to a function that implements a finite
state machine by examining attributes of its argument).

And then there are (or should be!) purists who will claim that if your
state machine requires information to be shared between states, then you
don't have enough states!  ;-)

> We want a goto.

No, we don't.


Dan Sommers

More information about the Python-list mailing list