Switch in Python

后端 未结 7 1150
孤独总比滥情好
孤独总比滥情好 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:24

    With "get" method, you can have the same effect as "switch..case" in C.

    Marcin example :

    switch_dict = {
        Foo: self.doFoo,
        Bar: self.doBar,
    }
    
    func = switch_dict.get(switch_var, self.dodefault)
    result = func() # or if they take args, pass args
    

提交回复
热议问题