Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 550383
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:16:58+00:00 2026-05-13T11:16:58+00:00

Related to this Stack Overflow question (C state-machine design) , could you Stack Overflow folks share

  • 0

Related to this Stack Overflow question (C state-machine design), could you Stack Overflow folks share your Python state-machine design techniques with me (and the community)?

At the moment, I am going for an engine based on the following:

class TrackInfoHandler(object):
    def __init__(self):
        self._state="begin"
        self._acc=""

    ## ================================== Event callbacks

    def startElement(self, name, attrs):
        self._dispatch(("startElement", name, attrs))

    def characters(self, ch):
        self._acc+=ch

    def endElement(self, name):
        self._dispatch(("endElement", self._acc))
        self._acc=""

    ## ===================================
    def _missingState(self, _event):
        raise HandlerException("missing state(%s)" % self._state)

    def _dispatch(self, event):
        methodName="st_"+self._state
        getattr(self, methodName, self._missingState)(event)

    ## =================================== State related callbacks

But I am sure there are tons of ways of going at it while leveraging Python’s dynamic nature (e.g. dynamic dispatching).

I am after design techniques for the “engine” that receives the “events” and “dispatches” against those based on the “state” of the machine.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T11:16:58+00:00Added an answer on May 13, 2026 at 11:16 am

    I don’t really get the question. The State Design pattern is pretty clear. See the Design Patterns book.

    class SuperState( object ):
        def someStatefulMethod( self ):
            raise NotImplementedError()
        def transitionRule( self, input ):
            raise NotImplementedError()
    
    class SomeState( SuperState ):
        def someStatefulMethod( self ):
            actually do something()
        def transitionRule( self, input ):
            return NextState()
    

    That’s pretty common boilerplate, used in Java, C++, Python (and I’m sure other languages, also).

    If your state transition rules happen to be trivial, there are some optimizations to push the transition rule itself into the superclass.

    Note that we need to have forward references, so we refer to classes by name, and use eval to translate a class name to an actual class. The alternative is to make the transition rules instance variables instead of class variables and then create the instances after all the classes are defined.

    class State( object ):
        def transitionRule( self, input ):
            return eval(self.map[input])()
    
    class S1( State ): 
        map = { "input": "S2", "other": "S3" }
        pass # Overrides to state-specific methods
    
    class S2( State ):
        map = { "foo": "S1", "bar": "S2" }
    
    class S3( State ):
        map = { "quux": "S1" }
    

    In some cases, your event isn’t as simple as testing objects for equality, so a more general transition rule is to use a proper list of function-object pairs.

    class State( object ):
        def transitionRule( self, input ):
            next_states = [ s for f,s in self.map if f(input)  ]
            assert len(next_states) >= 1, "faulty transition rule"
            return eval(next_states[0])()
    
    class S1( State ):
        map = [ (lambda x: x == "input", "S2"), (lambda x: x == "other", "S3" ) ]
    
    class S2( State ):
        map = [ (lambda x: "bar" <= x <= "foo", "S3"), (lambda x: True, "S1") ]
    

    Since the rules are evaluated sequentially, this allows a “default” rule.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 434k
  • Answers 434k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Something like this is the simplest and works (as seen… May 15, 2026 at 3:17 pm
  • Editorial Team
    Editorial Team added an answer First of all, you will need a DIV somewhere on… May 15, 2026 at 3:17 pm
  • Editorial Team
    Editorial Team added an answer Your first technique is actually the preferred method considering MVC2's… May 15, 2026 at 3:17 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.