This wiki is in the process of being archived due to lack of usage and the resources necessary to serve it — predominately to bots, crawlers, and LLM companies. Edits are discouraged.
Pages are preserved as they were at the time of archival. For current information, please visit python.org.
If a change to this archive is absolutely needed, requests can be made via the infrastructure@python.org mailing list.

State Programming

Why, When

Very often, the response of a function will depend on the state of this object. With this pattern, It's easy to do such a thing ! You just have to write several sub-classes, each per state, inherit the State class and call the setState when the object need to change state.

Code

   1 # Code is Public Domain.
   2 class State:
   3     
   4     def setState(self,stateClass):
   5         #print stateClass.__dict__
   6         for (name,attr) in stateClass.__dict__.iteritems():
   7             if not name.startswith('_'