As we all know, there is no switch in Python, which is generally replaced by if-else, for example, the C Language
Switch (key) {Case 'A':/* do_a */break; Case 'B':/* do_ B */break; Case 'C ': /* do_c */break ;}
In python
If key = 'A': # do_aelif key = 'B': # do_belif key = 'C': # do_c
If-else is simple enough and Practical enough, it can simulate multiple cases to accomplish the same thing, and the default situation.
However, some people like dict for implementation.
{'A': do_a, 'B': do_ B, 'C': do_c} [Key] (X)
However, the above implementation cannot simulate multiple cases to accomplish the same thing, barely implement default, but it is ugly.
Try: {'A': do_a, 'B': do_ B, 'C': do_c} [Key] (x) handle T keyerror: do_default
I also tried to use the class to implement one. I used the class to simulate some of the dict attributes to expand dict so that I could simulate multiple cases to accomplish the same thing and the default situation.
Class switch: def _ init _ (self, Data = {}): Self. data ={} for key in data. keys (): If type (key) = tupe: For k in key: Self. data [k] = data [Key] else: Self. data [Key] = data [Key] def _ setitem _ (self, key, item): Self. data [Key] = item def _ getitem _ (self, key): If key in self. data. keys (): return self. data [Key] else: return self. data ['default'] # End switch # is used. The following example shows a nested switch: Switch ({'A': Switch ({1: do_a_1, (2, 3 ): do_a_2, 'default': do_a}), 'B': Switch ({1: do_ B _1, (2, 3): do_ B _2, 'default': do_ B }), 'default': Switch ({'default': do_default}) [key1] [key2] ()
But after reading it, I still feel ugly. =!
I googled the Internet and found a big bull switch.
##{{ Http://code.activestate.com/recipes/410692/ (R8) # This class provides the functionality we want. you only need to look at # this if you want to know how this works. it only needs to be defined # once, no need to muck around with its internals. class switch (object): def _ init _ (self, value): Self. value = value self. fall = false def _ ITER _ (Self): "" return the match method once, then stop "" Y Ield self. match raise stopiteration def match (self, * ARGs): "" indicate whether or not to enter a case suite "If self. fall or not ARGs: Return true Elif self. value in ARGs: # changed for v1.5, see below self. fall = true return true else: Return false # The following example is pretty much the exact use-case of a dictionary, # But is wrongly ded for its simplicity. note that you can include statements # In each suite. V = 'ten 'for case in switch (V): If case ('one'): Print 1 break if case ('two '): print 2 break if case ('ten '): Print 10 break if case ('even'): Print 11 break if case (): # default, cocould also just omit condition or 'If true' print "something else! "# No need to break here, it'll stop anyway # Break is used here to look as much like the real thing as possible, but # Elif is generally just as good and more concise. # Empty suites are considered syntax errors, so intentional fall-throughs # shoshould contain 'pass' c = 'Z' for case in switch (c): If case ('A '): pass # Only necessary if the rest of the suite is empty if case ('B'): pass #... if case ('y '): Pass if case ('Z'): Print "C is lowercase! "Break if case ('A'): pass #... if case ('Z'): Print" C is uppercase! "Break if case (): # default print" I dunno what C was! "# As suggested by Pierre quentel, you can even expand upon the # functionality of the classic 'case' Statement by matching multiple # cases in a single shot. this greatly benefits operations such as the # uppercase/lowercase example above: Import stringc = 'A' for case in switch (c): If case (* string. lowercase): # note the * for unpacking as arguments print "C is lowercase! "Break if case (* string. uppercase): Print" C is uppercase! "Break if case ('! ','? ','. '): # Normal argument Passing Style also applies print "C is a sentence Terminator! "Break if case (): # default print" I dunno what C was! "# Since Pierre's suggestion is backward-compatible with the original recipe, # I have made the necessary modification to allow for the above usage. # End of http://code.activestate.com/recipes/410692 }}}
But looking back, I think it's better to use if-else honestly to do so many cool tasks for a switch. I just say, "It's a big step, it's a Big Bang ".
I just want to find a place to save so many things.Code.
* ** End ***