News for hackers. Stuff that matters.
Re:The death of if/else cascades, a lightweight alternative by amix on Sunday December 06, 2009 @10:21AM (#979623)
This is a common practice in Python and most of the implementations I have seen (and used) use it in a following way:

    handler = {
        'football': handle_football,
        'comedy': handle_comedy,
        'crime': hendle_crime
    }.get

    handler('crime')(...)
Re:The death of if/else cascades, a lightweight alternative by pushingbits on Sunday December 06, 2009 @11:10AM (#979695)
I use this all the time in C++.

  std::map<std::string, boost::function<void (int)>> handlerMap;

  boost::assign::insert(handlerMap) 
  ("football", &handleFootball)
  ("comedy", &handleComedy)
  ("crime", &handleCrime);

  handlerMap["football"](channelNumber);
Makes the code so much easier to understand. And when the handler functions start getting too complex, you can just add another level:

  handlerMap["comedy"]["situational_comedy"](channelNumber);
Re:The death of if/else cascades, a lightweight alternative by domnit on Sunday December 06, 2009 @11:05AM (#979688)
As a dynamic language, Python already has similar tables built in. You can use something like:

  getattr(self, 'handle_%s' % action)
Re:The death of if/else cascades, a lightweight alternative by kyleburton on Sunday December 06, 2009 @11:08AM (#979694)
If action comes from outside the program, this can open up unexpected behavior. The dispatch table is closed - there's less chance that user input will invoke something unintended.
Re:The death of if/else cascades, a lightweight alternative by domnit on Sunday December 06, 2009 @11:27AM (#979721)
In a real world program you would validate input and make sure you get something callable (with either technique). The only additional unexpected behavior is when there could be unexpected handle_xxx attributes. If we take for granted that the programmer defines the dict for the explicit dispatch table, though, we can also assume that the programmer defines the object for the implicit table.
Re:The death of if/else cascades, a lightweight alternative by kyleburton on Sunday December 06, 2009 @12:10PM (#979798)
IMO as code gets modified by other developers, the table is easier to keep 'safe' since it is clearer that is what it's for, the sprintf and all the other methods don't tend to be in one place - as methods get added, the reflection issue is oft overlooked.
Re:The death of if/else cascades, a lightweight alternative by eru on Sunday December 06, 2009 @11:53AM (#979765)
I feel the version from the original comment is most Pythonic. The other version is close to an eval-function, which seems to be frowned up on in Python.
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?
Re:The death of if/else cascades, a lightweight alternative by blasdel on Sunday December 06, 2009 @11:06AM (#979690)
It's especially common since Python purposefully doesn't have a fake switch/case statement -- if it can't be turned into a jumptable at compile-time, it ain't real!

The inane lisper bon mot is stated backwards: it should be "Data isn't code"