Get the app to ++, post and comment
  • Biggest annoyance of your fav language?
    Week 30 Group Rant
  • 33

    Why can't Python have a switch case statement???

    Also what's with the capitals in True/False?

  • 6
    If you really need a switch statement you can use a dictionary combined with a lambda function or an inline function.
  • 5
    Sure you can workaround in numerous ways but still a simple switch is quite useful from time to time.
  • 0
    @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.
  • 3
    the beauty of switch/case is the possibility with deliberate fall-throughs.
  • 1
    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 ¯\_(ツ)_/¯
  • 1
    Use 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!
  • 3
    @JohanO If you want your language to behave like C, you should probably just use C.
  • 0
    @Lisanna Oh if only they would let me do that :-)
  • 2
    @JohanO You'd be surprised how many people really hate this fall-through behavior.
  • 1
    @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
  • 1
    @Godisalie in compiled languages they are faster than long if else if chains
  • 2
    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.
  • 2
    @JohanO You mean the ugliness, unreadableness and unmaintainability?
Add Comment