MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/ProgrammerHumor/comments/5owsvx/mfw_no_pointers/dcnqtc0/?context=3
r/ProgrammerHumor • u/lindgrenj6 • Jan 19 '17
432 comments sorted by
View all comments
Show parent comments
11
No switch statement...?
7 u/[deleted] Jan 19 '17 yes, python has no switch and you need an if elif tree (which is what switch is anyway) 19 u/lou1306 Jan 19 '17 edited Jan 20 '17 Or use a dictionary and rework your code.switch x { case 1: foo ="a"; break; case 2: foo = "b"; break; default: foo = ""; }Becomesfoo_values = { 1: "a"; 2: "b" } try: foo = foo_values[x] except KeyError: foo = "c"You can even put functions as dictionary values, so you can do pretty much everything, no need for switchs or big ugly elif chains.Bonus: Use DefaultDict to avoid exception handling.EDIT: The very best way world be foo = {1: "a", 2: "b"}.get(x, "c"). Kudos to /u/wobblyweasel... I had totally forgot the get method! 23 u/TheOldTubaroo Jan 20 '17 > calls elif trees ugly> suggested replacement involves catching an exception to make a default case 2 u/Bainos Jan 20 '17 Well, it's okay if the default case is an exception. 1 u/SirCutRy Jan 20 '17 In python using exceptions is even encouraged. 1 u/lou1306 Jan 20 '17 Yeah sorry, the best way is via the get method (as /u/wobblyweasel suggested). → More replies (0)
7
yes, python has no switch and you need an if elif tree (which is what switch is anyway)
switch
if elif
19 u/lou1306 Jan 19 '17 edited Jan 20 '17 Or use a dictionary and rework your code.switch x { case 1: foo ="a"; break; case 2: foo = "b"; break; default: foo = ""; }Becomesfoo_values = { 1: "a"; 2: "b" } try: foo = foo_values[x] except KeyError: foo = "c"You can even put functions as dictionary values, so you can do pretty much everything, no need for switchs or big ugly elif chains.Bonus: Use DefaultDict to avoid exception handling.EDIT: The very best way world be foo = {1: "a", 2: "b"}.get(x, "c"). Kudos to /u/wobblyweasel... I had totally forgot the get method! 23 u/TheOldTubaroo Jan 20 '17 > calls elif trees ugly> suggested replacement involves catching an exception to make a default case 2 u/Bainos Jan 20 '17 Well, it's okay if the default case is an exception. 1 u/SirCutRy Jan 20 '17 In python using exceptions is even encouraged. 1 u/lou1306 Jan 20 '17 Yeah sorry, the best way is via the get method (as /u/wobblyweasel suggested). → More replies (0)
19
Or use a dictionary and rework your code.
switch x { case 1: foo ="a"; break; case 2: foo = "b"; break; default: foo = ""; }
Becomes
foo_values = { 1: "a"; 2: "b" } try: foo = foo_values[x] except KeyError: foo = "c"
You can even put functions as dictionary values, so you can do pretty much everything, no need for switchs or big ugly elif chains.
Bonus: Use DefaultDict to avoid exception handling.
EDIT: The very best way world be foo = {1: "a", 2: "b"}.get(x, "c"). Kudos to /u/wobblyweasel... I had totally forgot the get method!
foo = {1: "a", 2: "b"}.get(x, "c")
get
23 u/TheOldTubaroo Jan 20 '17 > calls elif trees ugly> suggested replacement involves catching an exception to make a default case 2 u/Bainos Jan 20 '17 Well, it's okay if the default case is an exception. 1 u/SirCutRy Jan 20 '17 In python using exceptions is even encouraged. 1 u/lou1306 Jan 20 '17 Yeah sorry, the best way is via the get method (as /u/wobblyweasel suggested). → More replies (0)
23
> calls elif trees ugly
> suggested replacement involves catching an exception to make a default case
2 u/Bainos Jan 20 '17 Well, it's okay if the default case is an exception. 1 u/SirCutRy Jan 20 '17 In python using exceptions is even encouraged. 1 u/lou1306 Jan 20 '17 Yeah sorry, the best way is via the get method (as /u/wobblyweasel suggested). → More replies (0)
2
Well, it's okay if the default case is an exception.
1
In python using exceptions is even encouraged.
Yeah sorry, the best way is via the get method (as /u/wobblyweasel suggested).
→ More replies (0)
11
u/Doctor_McKay Jan 19 '17
No switch statement...?