Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
If you really need a switch statement you can use a dictionary combined with a lambda function or an inline function.
-
JohanO20508ySure you can workaround in numerous ways but still a simple switch is quite useful from time to time.
-
@JohanO Why would people even use switches? If you've got if/elif it feels like you want to drive a trabbi when there is an easy to read ferrari in the garage. I never understood the appeal switch statements seem to have to some people.
-
I use boolean if-else whenever I feel the need to use a switch condition. I feel like a noob using switch statements and it doesn't give me a badass programmer feel ¯\_(ツ)_/¯
-
hiestaa4018yUse dicts bro where keys are cases and values are functions, it makes the code very clear. Use the `.get(key, default)` to have to equivalent of the default case in your switch statement.
If you define each function that is one branch of your switch case as a named one instead of a lambda expression, you'll see your code almost self documented by the name you choose. You'll see its much more concise, easier to read and more elegant than using a dirty switch! Also when your code branch will grow, you won't have to split up your dirty 80 lines functions into multiple ones, it will be done already and you'll feel like a badass programmer. Worth it! -
JohanO20508y@le717 hehe, I remember the days when I used to code (16-bit) windows apps in C, then switch() was _king_!
https://msdn.microsoft.com/en-us/...(v=vs.85).aspx -
Not having switch was a design decision, and IMO a very good one. Switch statements in general are a sign of bad design. If you need a switch in python, you are doing something very wrong.
Related Rants
Why can't Python have a switch case statement???
Also what's with the capitals in True/False?
undefined
wk30