Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
For 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. -
Oh god.
If there wasn't so strict whitespace rules you could simply:
if condition: true_result else: false_result -
Honestly 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 -
Suppose you are using in assignment:
variable = 1 if condition else 2
instead of:
variable = condition ? 1 : 2
I mean, you can clearly see which one is better 🤷🏻♂️ -
@AlgoRythm
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 -
I think it may be because of the usage in comphrehensions:
list = [x if x%2 == 0 else 10 for x in range(20)]
print(list) -
amoux2704yHow is it hard to comprehend simple logic? I don’t see anything wrong with it.
>> True if True else False
True
>> False if not False else True
False -
@amoux because it's more common that the condition is presented before the expression that is run. You might first read it as if true_result is always run because there's no condition before it.
-
stub1444yAs 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.
Related Rants
Whatever the fuck Python's ternary operator syntax is supposed to be.
rant
wk229
python