+ 6

Switch in python

well just a bit off topic but how many people thinks python must have switch case for true saying absence of switch case didn't bothered me but what are your opinions ?

1st Oct 2017, 7:16 PM
Prathamesh Khatavakar
Prathamesh Khatavakar - avatar
3 Answers
+ 7
In my opinion python doesnt need switch statements because it got something much better : a "Mapping table" so instead of the old: switch(val){ case 1: do1() break; case 2: do2() break; default: do3() } you can use the much better table: x = the value you want to use mydefault = 3 { 1 : do1, 2: do2, 3: do3, }.get(x,mydefault)()
1st Oct 2017, 7:23 PM
Chrizzhigh
Chrizzhigh - avatar
+ 3
if and Elifs are so short that there is no need for switch in python like other languages
2nd Oct 2017, 9:43 AM
Ore
Ore - avatar
+ 2
I don't see the need for a switch. The switch can essentially done with if-elif-else statements (and while loop if needed)
1st Oct 2017, 11:37 PM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar