News for hackers. Stuff that matters.
Re:The death of if/else cascades, a lightweight alternative by cma on Sunday December 06, 2009 @12:14PM (#979812)
The second version is slower too. Here's what I use for e.g. a state machine, which follows "don't repeat yourself" a little better than the first example:

  # {state_name: state_action}
  states = {}
  
  state = ['start']
  
  def reg(func):
    states[func.__name__] = func
    return func
  
  # define and simultaneously register the states
  @reg
  def start():
    state[0] = 'second'
  
  @reg
  def second():
    state[0] = 'last'
  
  @reg
  def last():
    state[0] = None
  
  
  # run the state machine
  while state[0]:
    print state[0]
    states[state[0]]()
Re:The death of if/else cascades, a lightweight alternative by eru on Sunday December 06, 2009 @01:42PM (#979939)
Is the second version slower because the string won't get intern-ed?
Re:The death of if/else cascades, a lightweight alternative by cma on Sunday December 06, 2009 @02:07PM (#979975)
Yeah, and the string must be reinterpolated every time (possibly barring some of the more exotic python implementations).
Re:The death of if/else cascades, a lightweight alternative by eru on Thursday December 10, 2009 @12:50PM (#988182)
Why are you using mutation?