[EMAIL PROTECTED] a écrit :
> 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
> 

NameError here !-)

> 
> but I hate to use global variable.

<aol />

> 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()


A possible solution:

# state_machine.py
class _StateMachine(object):
   def __init__(self):
     self._state = SomethingHere()
   def get_state(self):
     return self._state
   def set_state(self, xxx):
     # etc

_s = _StateMachine()
get_state = _s.get_state()
set_state = _s.set_state()

You still have a global, but at least it's a machine instance, not it's 
state !-)

Now the question is: why do you think it's so important for your users 
to only see functions ? What's so wrong with:

from state_machine import *
m = get_state_machine()
m.set_state(42)
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to