20 comments

[ 2.0 ms ] story [ 52.4 ms ] thread
Hmm, to me it looks confusing however it’s broken down, but I’m not a python programmer. Is there no switch/case expression that would be preferable?
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).

In Ruby, everything is an expression. I also miss Ruby’s unless in many languages.
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.
You can dispense with the backslashes if you put the entire if-expression in parentheses.
1) If you format your code more nicely it becomes easier to read. I don't think this has anything to do with pythons ternary operator.

2) I also like it. Mostly because it was missing for so long and that has always been very annoying.

I admit it. I find it retarded
Compared to C's ternary operator [1] it seems the Python version operates on 5 items.

[1] https://en.m.wikipedia.org/wiki/Ternary_operation

Isn't it just two nested ternary operators? The equivalent being:

  selected_value =
    condition_1 ? 
      value_1 :
      condition_2 ? value_2 : value_3
Ah, of course - thanks for clarifying
The C (and C++) ternary operator is usually used in a more convenient way to handle multiple cases:

  selected_value =
    condition_1 ? value_1 :
    condition_2 ? value_2 : 
        value_3
This reminds me of what 'when' is like in VHDL:

    mySignal <= "1111" when some_state = '1' else "0011"
Rule of thumb: optimize for readability