State Design Pattern in Python
Not a proper code review as per se but I was wondering if things couldn't be done in a more straight-forward way like this :

#!/usr/bin/python

class Machine(object):
    def __init__(self, state, transitions):
        self.transitions = transitions
        self.state = state

    def __call__(self, input):
        old_state = self.state
        self.state = self.transitions.get((self.state, input), self.state)
        print("DEBUG %s -- %s --&gt; %s" % (old_state, input, self.state))

machine = Machine(
    "a",
    {
        ("a","msgA"):"b",
        ("b","msgB"):"c",
        ("c","msgB"):"b",
        ("c","msgC"):"a",
    }
    )

machine("msgA")
machine("msgB")
machine("msgC")
machine("msgB")


Using functions to have actual side effects, this could look like this :

#!/usr/bin/python


# examples of helper functions
def goto(s):
    return lambda state,input: s

def no_move():
    return lambda state,input: state

def verbose_no_move():
    def func(state,input):
        print('Cannot find a transition for %s from %s' % (input, state))
        return state
    return func

def print_input_and_goto(s):
    def func(state,input):
        print(input)
        return s
    return func

def print_old_state_and_goto(s):
    def func(state,input):
        print(state)
        return s
    return func

def raise_exception():
    def func(state,input):
        raise ValueError('Cannot find a transition for %s from %s' % (input, state))

class Machine(object):
    def __init__(self, initial_state, transitions):
        self.transitions = transitions
        self.state = initial_state
        self.default = verbose_no_move() # pick a default behavior

    def __call__(self, input):
        old_state = self.state
        func = self.transitions.get((self.state, input), self.default)
        self.state = func(self.state,input)
        print("DEBUG %s -- %s --&gt; %s" % (old_state, input, self.state))


machine = Machine(
    "a",
    {
        ("a","msgA"):goto("b"),
        ("b","msgB"):print_input_and_goto("c"),
        ("c","msgB"):goto("b"),
        ("c","msgC"):print_old_state_and_goto("a"),
    }
    )

machine("msgA")
machine("msgB")
machine("msgC")
machine("msgB")