state machine and a global variable

Chris Mellon arkanes at gmail.com
Fri Dec 14 13:34:33 EST 2007


On Dec 14, 2007 10:52 AM,  <tuom.larsen at gmail.com> wrote:
> Dear list,
> I'm writing very simple state machine library, like this:
>
>
>
> _state = None
>
> def set_state(state):
>     global _state
>     _state = state
>
> def get_state():
>     print _surface
>
>
>
> but I hate to use global variable. So, please, is there a better way
> of doing this? All I want is that a user has to type as little as
> possible, like:
>

Yes, don't do it.

> from state_machine import *
> set_state(3)
> get_state()
>

This isn't a state machine, it's just state

> I.e., nothing like:
> import state_machine
> my_machine = state_machine.new_machine()
> my_machine.set_state(3)
> my_machine.get_state()
>

Stop being afraid of objects, especially if your fear is caused by
straw-man C style APIs.

from state_machine import State

state = State()
print state.current_state
state.do_something('sadfsafsad') #does something based on the current state
print state.current_state



More information about the Python-list mailing list