i was writing up some code in python and had several if/else like statements that i figured would require a better syntax and figured that the switch/case method would possibly be a better way to handle it. lo and behold, after research…there’s no switch/case like statement!!

i found several ways of solving the issue, which seem to be rather interesting. many people thought that the if/else was probably the best way to go…of course we can’t have that, and continued to find some people stating to use the dict like command to determine your call. for example:

def performAction1():
   print “some crap”
def performAction2():
   print “some more crap”
{
   ”action1″: performAction1,
   ”action2″: performAction2,
   …etc…
}[actionToPerformString]()

this seemed to be a good way to do it, but something about the actionToPerformString trailing like that bothered me, so there was also this method which seemed to be more visually pleasing to me:

def defaultActionToPerform():
   print “default crap”
action = {
   ”action1″: performAction1,
   ”action2″: performAction2,
   …etc…
}
action.get(actionToPerformString, defaultActionToPerform)()

as you can see, it also allowed for a default function to be called, plus it reall just does look cleaner. if anybody has a more elegant solution, please feel free to comment.