Why doesn't Python have switch-case?

前端 未结 4 1308
遇见更好的自我
遇见更好的自我 2021-01-30 08:17

Please explain why Python does not have the switch-case feature implemented in it.

4条回答
  •  时光取名叫无心
    2021-01-30 09:03

    def f(x):
        return {
            1 : 'output for case 1',
            2 : 'output for case 2',
            3 : 'output for case 3'
        }.get(x, 'default case')   
    

    You can use this as switch case in python and if condition not match it will return default if condition not match

提交回复
热议问题