It's a bit weird that Python -- which in some ways was a trailblazer in popularizing somewhat unconventional syntax choices (indent matters!) -- has become so stuck in its ways that if-elif seems like a reasonable choice of syntax for what is usually a generalized switch[1].
[1]. The MultiWayIf extension for GHC is a great example of how to optimize this idiom for regularity.
switch/case was, and still is, considered unnecessary in Python because it can be expressed just as clearly and concisely using if/elif/else. There was discussion about adding it, long ago, but (IIRC) Guido decided against it. It is not obvious what syntax switch/case should have in Python (two levels of indentation?), and its benefits are marginal at best.
Then again, many things that supposedly would "never" make it into the language, have been added over time anyway, including the ternary if. So who knows...
> switch/case was, and still is, considered unnecessary in Python because it can be expressed just as clearly and concisely using if/elif/else
Ah, but it seems that there's some disagreement about this being unnecessary :).
Btw, MultiWayIf is a much more powerful (bad word choice?) than 'switch' because it's more flexible at no additional syntactic cost. FWIW, I mostly just like it because of the extreme syntactic regularity of MWI.
(I have no horse in the race. I just use Python occasionally as a scripting language, but I've been moving over to Haskell+Stack for scripts, so... )
i use this in ruby and thought it was fine at first. but after looking back at older code or code others have written i have changed my mind and think it is usually better to avoid it.
i think it makes it more complicated to think about because it requires some backtracking in thought
Ruby's if/elsif/else statement returns a value making it a nice alternative to trenary operators. I have for the world not understood why one would do it otherwise.
Unless you want to use mutation you have to use it in python, and then you end up with ugly chains if you want brevity.
If it returns a value then wouldn't it be an if expression, not an if statement?
It certainly sounds like a useful feature, as someone who writes a lot of JavaScript I can see the value (and I believe a form of this is coming to JS in the form of `do` expressions).
I find it usually makes lines really long, especially if you avoid usage of \ like a lot of style guides recommend. The longer lines usually end up more difficult to read than a simple regular if statement.
rust solved this itch in a wonderful way via the match operator. match has some powerful semantics that let it select branches beyond just a pure bool in the conditional expression (e.g. value in some range?)
[1]. The MultiWayIf extension for GHC is a great example of how to optimize this idiom for regularity.