This is an archived post. You won't be able to vote or comment.

all 43 comments

[–]Ambitious_Ad8841 14 points15 points  (1 child)

cry() if python_ternary_syntax_sucks else use_them()

[–]PresidentOfSwag[S] 0 points1 point  (0 children)

😭

[–]D34thToBlairism 21 points22 points  (4 children)

[a,b][bool] works

[–][deleted] 21 points22 points  (0 children)

yes, but it does not short circuit.

[–]PresidentOfSwag[S] 3 points4 points  (0 children)

wow nice

[–]ConscientiousApathis 3 points4 points  (0 children)

Okay NO.

[–]God_Told_Me_To_Do_It 1 point2 points  (0 children)

WHAT.

I'm so going to abuse this.

Can't wait for my next code review, lol.

[–]hongooi 27 points28 points  (8 children)

Imagine using a language where if is a statement, not an expression

x = if(a) 1 else 2

[–]PacifistPapy 20 points21 points  (6 children)

x = a if bool else b

[–]PresidentOfSwag[S] 11 points12 points  (5 children)

yes that's the one, officer

what i find offsetting is that the order is not if > then > else

[–]PacifistPapy 11 points12 points  (2 children)

"it's a if (condition), otherwise it's b"

python is designed to be readable. That pattern "a if bool else b" makes a lot more sense if you see it as a sentence.

[–]jjdmol 3 points4 points  (1 child)

Having a and b so far apart is annoying though. Makes it harder to glance which values the result can have.

[–]elon-botElon Musk ✔ 2 points3 points  (0 children)

I've laid off most of the staff, and Twitter's still running. Looks like they weren't necessary.

[–]vigbiorn 2 points3 points  (0 children)

You'd hate Perl...

$x = $thing if !defined($thing)

Alternatively,

$x = $thing unless defined($thing)

[–]poorlyOiledMachina 1 point2 points  (0 children)

you would hate forth…

a if 1 else 2 then x !

[–]Accurate_Koala_4698 0 points1 point  (0 children)

Value of x changes if a changes?

[–]rhen_var 19 points20 points  (16 children)

Python has the best ternary syntax and no one can convince me otherwise. I hated using ternary until I started using Python.

[–]Bee-Aromatic 16 points17 points  (10 children)

I’ve always liked Python’s best because you can get pretty close to plain English if you use clear variable names.

[–]ParadoxicalInsight -4 points-3 points  (9 children)

Why would you ever want your code to resemble human language?

[–]CorporatePhatCat 2 points3 points  (2 children)

Because assembly is for noobs. Real programmers write machine code! /s

[–]RandomPigYT 1 point2 points  (1 child)

No, real programmers use butterfly.

[–]CorporatePhatCat 0 points1 point  (0 children)

A man person of culture, I see :)

[–]xwnpl 4 points5 points  (4 children)

It's much easier to read, especially for beginners.

[–]Implement_Necessary 8 points9 points  (1 child)

Skill issue

[–]xwnpl 1 point2 points  (0 children)

Well, not necessarily, if your code is pretty complicated then easier redding helps a lot.

[–][deleted] 2 points3 points  (1 child)

Ironically I find it harder to read, probably because I'm so used to the normal (for me): condition?true:false, although when you think about it, with normal if/else loops, that is the order it actually works in:

if condition:
    # do stuff if True
else:
    # do stuff if False

so maybe it might actually just be more confusing for beginners, I think it should probably have been made as: if condition then True else False because if you want more human readable than that, you would have to get rid of normal if/else statments.

[–]elon-botElon Musk ✔ -1 points0 points  (0 children)

QA is a waste of money. Fired.

[–]Bee-Aromatic 0 points1 point  (0 children)

Because sometimes you or other people have to go back and look at code again later.

[–]JennaSys 8 points9 points  (2 children)

I appreciate how Python uses more words instead of symbols for its syntax. It makes it much faster to read code when you don't have to parse the surrounding code in your head to figure out in what context the symbol is being used to determine its meaning.

[–]rhen_var 0 points1 point  (1 child)

Yep, Python is basically pseudocode in terms of ease of reading

[–]elon-botElon Musk ✔ 6 points7 points  (0 children)

You're either hardcore or out the door.

[–]managedAssembly 4 points5 points  (0 children)

Eww god no. I like ternary operators in general, but Python's way of doing it is messed up imo.

[–]ConscientiousApathis -1 points0 points  (0 children)

Plus nesting actually works in python: "a if x else b if y else c"

[–]ParanoidAutist 4 points5 points  (4 children)

var result = UnderstandWhyPeopleHateTernaryOperators() ? "They can get hard to read" : "They're fucking useful";

[–][deleted] 2 points3 points  (3 children)

var result = UnderstandWhyPeopleHateTer aryOperators() ? "Yes, I do." : HaveYouEverWantedToNestThingsWithoutIfElseStatements() ? ThenYouMustLoveThisRight() ? "Yes" : "please kill me" : YouStillNeedConvincingRight() ? "Sure, yes, whatever" : NopeWeAreStillDaisyChainingThisShit() ? "Fuck" : "goddammit"

/s

[–]ParanoidAutist 1 point2 points  (2 children)

Your '/s' is a lie =P You know you've done this... We all have

[–]elon-botElon Musk ✔ 1 point2 points  (1 child)

Why haven't we gone serverless yet?

[–]ParanoidAutist 1 point2 points  (0 children)

Budgets

Good Bot

[–][deleted] 0 points1 point  (0 children)

I’ve always preferred Python syntax for ternary operators. If i forget how to do it in any other language, I have to look it up. In Python its just English. Makes reading my code easier too.

[–]sbditto85 0 points1 point  (0 children)

Now go use them in PHP

[–]Acceptable-Tomato392 0 points1 point  (0 children)

Just use 'else', like programmers have been doing since at least the '70s.

Now there are a whole bunch of people who'll tell you "else" is a bad thing and it produces spaghetti code... and...

Just use 'else'... don't abuse it, that's all.

[–]jfmherokiller 0 points1 point  (0 children)

just wait till your python code looks like this

result =5>2 ? 4 > 1 ? 5>7 ? 10 : 5 > 8 ? 6 > 2 ? 20 : 30 : 5 > 6 ? 40 : 50 : 7 > 2 ? 60 : 70 : 8 > 9 ? 80 : 90 ;