Do all the things like ++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatarSign Up
sudo-woodo50423dFor the uninitiated, ternary syntax in literally any other language is:
condition ? true_result : false_result
In Python it's:
true_result if condition else false_result
Like goddamn, I get that it's closer to how we'd make such a statement in natural language but from a programming point of view it's awful, if only for the fact that the condition is sandwiched inbetween the results.
RememberMe1411023dAgreed, I like Python in general but this is just awful.
If there wasn't so strict whitespace rules you could simply:
if condition: true_result else: false_result
AlgoRythm4844023dHonestly I think if the rest of the industry did it this way, it would be better. The ternary operator is infamously unreadable. But the Python operator is:
- Based on math
- Reads like English
I actually love it. But it's different from every other goddamn language so I always need to Google it
I do agree that the order is weird. Though it's somewhat better than the default order
theabbie285223dit's more like a one liner if-else statement
michezio186923dSuppose you are using in assignment:
variable = 1 if condition else 2
variable = condition ? 1 : 2
I mean, you can clearly see which one is better 🤷🏻♂️
yeami223dI like Python ternary syntax
I respect your opinion, but I think you're wrong.
Is the Python one based on English? Yes
Is the "normal" ternary operator difficult to read? Yes
Can I handle the change, or will my head explode? Change would kill me.
That's the only argument I have against what you wrote
uyouthe1612923dMeanwhile in Clojure “if” is just
(if condition true false)
Demolishun864823dI think it may be because of the usage in comphrehensions:
list = [x if x%2 == 0 else 10 for x in range(20)]
pxeger47322dAbsolutely not worth arguing about. You're all wrong and it doesn't matter one bit.
rooter66422dYh, i prefer ?: too. But python doesn't use many special characters in general. It would be bit inconsistent
amoux18519dHow is it hard to comprehend simple logic? I don’t see anything wrong with it.
>> True if True else False
>> False if not False else True
stub15616dAs a person just learning Python I like it. I prefer a language that tries to be more like the spoken word. For me Python seems to be easier to retain as well. I've been through tutorials for C# and JS and I didn't like them near as much. But if I was proficient in another language first I'd probably feel the same way you do.