Here is yet another way to emulate a switch-case statement, perhaps one you might not have thought of.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
import sys class case_selector(Exception): def __init__(self, value): # overridden to ensure we've got a value argument Exception.__init__(self, value) def switch(variable): raise case_selector(variable) def case(value): exclass, exobj, tb = sys.exc_info() if exclass is case_selector and exobj.args == value: return exclass return None def multicase(*values): exclass, exobj, tb = sys.exc_info() if exclass is case_selector and exobj.args in values: return exclass return None if __name__ == '__main__': print def InputNumber(): while 1: try: s = raw_input('Enter an integer') except KeyboardInterrupt: sys.exit() try: n = int(s) except ValueError, msg: print msg else: return n while 1: n = InputNumber() try: switch(n) except ( case(1), case(2), case(3) ): print "You entered a number between 1 and 3" except case(4): print "You entered 4" except case(5): print "You entered 5" except multicase(6, 7, 8, 9): print "You entered a number between 6 and 9" except: print "Youe entered a number less then 1 or grater then 9"
A switch-case statement (or the lack of it) seems to be a controversal point among Python users. There are several dictionary-based substitute proposals, and even one that quite closely emulates the C syntax (using a FOR loop!!!; you need to take a look at this one, it's brilliant and it's on http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/410692 ).
I personally do not extensivelly use switch-case constructs in any language, so I do not feel real pain about Python not supporting it. To me, the really interesting aspect of those substitutes is that they demonstrate what is all possible with Python, showing Python's power when it comes to "using it as a little language" (i.e. adding constructs in Python itself that emulate features which are hard-coded parts of other languages).
Some words about this recipe: Of cource it seems (very) weird to see except clauses instead of case selectors. But, on the other hand, how weird might statements like these look to a C or VB programmer:
return (lambda x: sqrt(-x), sqrt)[x >= 0] return (x < 0 and [sqrt(-x)] or [sqrt(x)])
(And these are definitely not the weirdest possible.)
After all, we're all creatures of habit, aren't we?
Cheers and happy switching!