Switch in Python

后端 未结 7 1127
孤独总比滥情好
孤独总比滥情好 2021-01-01 22:34

I have tried making a switch like statement in python, instead of having a lot of if statements.

The code looks like this:

def findStuff(cds):
    L         


        
7条回答
  •  渐次进展
    2021-01-01 23:34

    I don't know which article you've found to do something like this, but this is really messy: the whole result diction will be always evaluated, and instead of doing only part of the work (as a switch / if do), you'll do the whole work everytime. (even if you use only a part of the result).

    Really, a fast switch statement in Python is using "if":

    if case == 1:
      pass
    elif case == 2:
      pass
    elif case == 3:
      pass
    else:
      # default case
      pass
    

提交回复
热议问题