[EMAIL PROTECTED] wrote: > Basically, I agree that often the local state is much more useful. It > just seems to me that for some application it's an overkill. Like say, > for Turtle [1] (no jokes, please :) or PostScript [2].
Sounds also a bit similar to what happens under the hood in Open GL and some other systems which revolve around display lists. The approach generally taken there is to recognise that you often have a "current" context which is being operated on. A basic version of this might look like this: file: statemachine_user.py #!/usr/bin/python from statemachine import * context = statemachine() context2 = statemachine() set_context(context) set_state(3) print get_state() set_context(context2) set_state(1) print get_state() set_context(context) print get_state() file: statemachine.py #!/usr/bin/python class statemachine(object): def __init__(self): self.state = None def set_state(self, value): self.state = value def get_state(self): return self.state context = statemachine() def set_context(somecontext): global context context = somecontext def set_state(value): context.set_state(value) def get_state(): return context.get_state() A more interesting example which probably relates closer to your example, and also can be quite useful for interpretting little languages is where the "states" that get stored are matrices representing transforms and are used to put objects into a 3D space. You might in that circumstance want to use your a statemachine more like this: (this code is untested) class statemachine(object): default = None def __init__(self, **argd): self.__dict__.update(**argd) self.state = self.default def set_state(self, value): self.state = value def get_state(self): return self.state context = [ statemachine() ] # default context def push_context(somecontext): global context context.append( somecontext ) def pop_context(somecontext): global context return context.pop( -1 ) def set_state(value): context[-1].set_state(value) def get_state(): return context[-1].get_state() def get_allstates(): return [ x.get_state() for x in context ] This isn't really quite what the do, but gives a different possible perspective. Michael. -- http://yeoldeclue.com/blog http://kamaelia.sourceforge.net/Developers/ -- http://mail.python.org/mailman/listinfo/python-list