7 ms·
I admit it: I like Python’s ternary operator
- skywhopper 8y agoHmm, 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?
- Quekid5 8y agoIt'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.
- zephyrfalcon 8y agoswitch/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...
- Quekid5 8y ago> 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... )
- q3k 8y agohttps://github.com/q3k/switch4python https://github.com/q3k/switch4python :)
- asdf333 8y agoi 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
- bjoli 8y agoRuby'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.
- underwater 8y agoIf 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).
- rad_gruchalski 8y agoIn Ruby, everything is an expression. I also miss Ruby’s unless in many languages.
- lgunsch 8y agoI 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.
- curtis 8y agoYou can dispense with the backslashes if you put the entire if-expression in parentheses.
- jackewiehose 8y ago1) 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.
- charlyjaja 8y agoI admit it. I find it retarded
- dcassett 8y agoCompared to C's ternary operator [1] it seems the Python version operates on 5 items. [1] https://en.m.wikipedia.org/wiki/Ternary_operation https://en.m.wikipedia.org/wiki/Ternary_operation
- underwater 8y agoIsn't it just two nested ternary operators? The equivalent being: selected_value = condition_1 ? value_1 : condition_2 ? value_2 : value_3
- claudiawerner 8y agoThis reminds me of what 'when' is like in VHDL: mySignal <= "1111" when some_state = '1' else "0011"
- miguelmota 8y agoRule of thumb: optimize for readability
- cdaringe 8y agorust 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?) https://doc.rust-lang.org/book/ch06-02-match.html https://doc.rust-lang.org/book/ch06-02-match.html