state machine and a global variable

Carl Banks pavlovevidence at gmail.com
Fri Dec 14 16:43:29 EST 2007


On Dec 14, 11:52 am, tuom.lar... 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:
>
> from state_machine import *
> set_state(3)
> get_state()
>
> I.e., nothing like:
> import state_machine
> my_machine = state_machine.new_machine()
> my_machine.set_state(3)
> my_machine.get_state()
>
> Thanks, in advance!

I would recommend you do it the long way, but then provide convenience
functions for users who'd prefer a simplified interface:

def set_state(x):
    my_machine.set_state(x)

def set_state():
    return my_machine.get_state()


Some users won't want the simplified interface.  Really.


Carl Banks



More information about the Python-list mailing list