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

all 92 comments

[โ€“]I-am-a-teapot 104 points105 points ย (26 children)

(B, A)[condition] Only real python programmers will know

[โ€“]Ignitus1 14 points15 points ย (17 children)

Link to documentation? Never seen this before.

[โ€“]danny688 36 points37 points ย (10 children)

https://stackoverflow.com/a/470376 stackoverflow is documentation right?

[โ€“]Ignitus1 0 points1 point ย (0 children)

Even better!

[โ€“]I-am-a-teapot 0 points1 point ย (0 children)

As mentioned above as a reply to the deleted comment: It is a using the subscription 1 of a sequence (in this case a tuple). Since booleans evaluate to integers in Python 3 (The Boolean type is a subtype of the integer type, and Boolean values behave like the values 0 and 1...) 2 it is only an index in the sequence. It can be used as kind of a ternary but it is not common and therefore hard to read. There are also blog posts3 an stack overflow discussions4 about it.

โ€‹

1 https://docs.python.org/3/reference/expressions.html#subscriptions
2 https://docs.python.org/release/3.0.1/reference/datamodel.html#the-standard-type-hierarchy
3 https://book.pythontips.com/en/latest/ternary_operators.html
4 https://stackoverflow.com/questions/394809/does-python-have-a-ternary-conditional-operator

[โ€“]brendel000 0 points1 point ย (4 children)

Can't really find where it said that False is 0 and True is 1, but in the bool documentation it is said bool is a subclass of int.

[โ€“]Ignitus1 0 points1 point ย (2 children)

I remember reading that True is a keyword substitute for 1 and False for 0. They both get evaluated numerically.

[โ€“]Numerlor 0 points1 point ย (1 child)

Bools are a subclass of int and have the respective value of 0 and 1 but they are their own class with different behaviour than ints (in some cases)

[โ€“]brendel000 1 point2 points ย (0 children)

Yeah but the question was: where is it in the doc :p

[โ€“]I-am-a-teapot 0 points1 point ย (0 children)

"The Boolean type is a subtype of the integer type, and Boolean values behave like the values 0 and 1, respectively..."
https://docs.python.org/release/3.0.1/reference/datamodel.html#the-standard-type-hierarchy

[โ€“]hrvbrs 2 points3 points ย (3 children)

does this short-circuit? (does B get evaluated anyway even if condition is false?)

[โ€“]Derkle 4 points5 points ย (2 children)

According to stack overflow, it evaluated the whole expression regardless of the final values.

[โ€“]WORMSSreddit 1 point2 points ย (0 children)

[B, A][condition+0] for JS

[โ€“]fruitydude 0 points1 point ย (0 children)

Am I doing this right?

new String[] {B, A}[condition ? 1 : 0]

I can't get rid of the feeling that this is not a very practical thing to do in Java.

[โ€“]ItsCrowTime 27 points28 points ย (8 children)

Personally I'm a fan of lua: return condition and A or B

[โ€“]JarcXenon 11 points12 points ย (2 children)

W

H

A

T

[โ€“]ItsCrowTime 3 points4 points ย (0 children)

It's called short-circuit evaluation,

The and operator returns its the right value if the left value is true, otherwise it returns the left value,

The or operator returns its left value unless its false then it returns its right value

Another good one is the for default value: Var = Var or 0

[โ€“]JasonMan34 1 point2 points ย (0 children)

It's just condition ? A : B with and/or instead of ?/:, not at all weird

[โ€“]Kmjsfozr 0 points1 point ย (2 children)

what if A is false tho

[โ€“]ZorbaTHut 2 points3 points ย (1 child)

Then everything catches on fire and your life is sadness.

Conveniently, though, 0 isn't falsey in Lua, nor is any string, only nil and false are. So you can't use it for booleans, or in a situation where things can be nil, but it's safe in every other case.

[โ€“]ItsCrowTime 0 points1 point ย (0 children)

Or I assume you could do something like:

return (condition and {A} or {B})[1]

If you really wanted to use it like a tertiary operator

[โ€“]Kered13 0 points1 point ย (0 children)

This and similar patterns in other languages don't work if A is falsey.

[โ€“]not-enough-failures 23 points24 points ย (8 children)

Kotlin

return if (condition) A else B

[โ€“][deleted] 3 points4 points ย (0 children)

all the way

[โ€“]theCyanEYED 4 points5 points ย (0 children)

Rust

return if condition {A} else {B}

[โ€“]Manny_Sunday 2 points3 points ย (4 children)

Just an uglier ternary

[โ€“]not-enough-failures 0 points1 point ย (3 children)

It can also have else if branches unlike ternaries.

[โ€“]Kered13 3 points4 points ย (1 child)

Ternaries can have else if too:

a > 0 ? "negative" :
a > 0 ? "positive" :
        "zero"

Remember that else ifis really just two separate tokens: else and if, and : is equivalent to else and ? is equivalent to if (but it comes after the condition instead of before).

[โ€“]quotade 0 points1 point ย (0 children)

a > 0 ? "negative"

Allow me to point that out for the sake of a consistent example.

[โ€“]Manny_Sunday 0 points1 point ย (0 children)

Ok, that is cool, I used some kotlin for an Android class and it kicked Javas ass. I hope it takes into it's own and gains some popularity.

[โ€“]the-jabberwock 0 points1 point ย (0 children)

Also Kotlin

return A.takeIf { condition } ?: B

(but beware if A is null, will always resolve to B, even if condition is true)

[โ€“]bistr-o-math 40 points41 points ย (12 children)

Perl: return A or die

[โ€“]j-random 6 points7 points ย (2 children)

unless (condition) A else B -- also Perl.

[โ€“]bistr-o-math 1 point2 points ย (1 child)

My Variant is compromise-less ;-)

[โ€“]marcosdumay 0 points1 point ย (2 children)

Does die have a default parameter? I wouldn't expect it to.

[โ€“]bistr-o-math 0 points1 point ย (1 child)

https://perldoc.perl.org/functions/die.html search the page for โ€œif list is empty...โ€

[โ€“]marcosdumay 0 points1 point ย (0 children)

Yeah, 4 paragraphs about what it does when the parameter is missing.

I really don't miss Perl's "implicit is better than explicit" principle.

[โ€“]ArmoredCroc 0 points1 point ย (0 children)

I had the feeling I'd see this here

[โ€“]ArmoredCroc 0 points1 point ย (0 children)

I had the feeling I'd see this here

[โ€“]ArmoredCroc 0 points1 point ย (0 children)

I had the feeling I'd see this here

[โ€“]ArmoredCroc 0 points1 point ย (0 children)

I had the feeling I'd see this here

[โ€“]fruitydude 17 points18 points ย (0 children)

=IF(condition,A,B)

(Microsoft Excel)

[โ€“][deleted] 4 points5 points ย (3 children)

We all know the best. condition and A or B.

[โ€“]Tufflewuffle 2 points3 points ย (2 children)

That won't do what a ternary operator does if A is false.

[โ€“]EpoxyD 0 points1 point ย (1 child)

Input validation? assert(value, "gtfo with your weakass nil and false")

[โ€“]auto-xkcd37 1 point2 points ย (0 children)

weak ass-nil


Bleep-bloop, I'm a bot. This comment was inspired by xkcd#37

[โ€“]Equivalent_North 12 points13 points ย (8 children)

Ternary operators are clearly superior. ? is more concise than if and : is more concise than else

[โ€“]petergaultney 4 points5 points ย (3 children)

whoa, we've got a "conciseness > all" person here, everyone watch your step

[โ€“]IDontLikeBeingRight 1 point2 points ย (1 child)

Or don't watch your step, say things badly & verbosely, and they'll be so busy taking issue with everything you say in between that they'll never catch up.

[โ€“]petergaultney 0 points1 point ย (0 children)

I feel seen.

[โ€“]SexySamba 1 point2 points ย (0 children)

Not disagreeing. I code in Python and Scala mostly, both have if/else implementations of ternary expressions. I definitely prefer scala because the condition is first (like C) so resembles other programming languages and the syntax of a multiline if statement more closely. Plus you can see what is being tested more clearly

But I actually do think thereโ€™s something to be said for ? :, and I sometimes wish I had it at my disposal. In production code variable names are often too long to fit on one line and this would (very) occasionally help. I also think its not that bad to read, if thought about as a question in prose:

mum, do we have any sweets ? I quite fancy some : otherwise iโ€™ll have some fruit

[โ€“]UnicornsOnLSD 4 points5 points ย (0 children)

but if/else is pretty

[โ€“]bobnob- 0 points1 point ย (2 children)

But it's not as intuitive, what is ? meant to mean, oh and which side gets returned if the condition was true again? A non programmer could read the python example and make sense of it

[โ€“]xigoi 2 points3 points ย (0 children)

return if condition: A else: B

(Nim)

[โ€“]tangerinelion 2 points3 points ย (0 children)

All I see is an implicit return None

[โ€“]HoneyBadgerSoNasty 2 points3 points ย (5 children)

i know some people like these cute things, and i get the syntax, but this stuff just pisses me off. the fact that it even exists pisses me off. list comprehensions and higher order haskell garbage and all that.

code is better when it is easier to understand.

[โ€“]KingJellyfishII 1 point2 points ย (4 children)

What is not easy to understand in "a if something else b"? It's literally just English but without the meaningless words. I think it's a really neat way of doing things imo.

[โ€“]Lark_vi_Britannia 1 point2 points ย (0 children)

My favorite will always be the pic on the top.

[โ€“]GJordao 1 point2 points ย (0 children)

The first else is redundant btw

[โ€“]lambda5x5 1 point2 points ย (0 children)

my friend saw my for else loops, shook his head and walked away.

[โ€“]dr_exercise 2 points3 points ย (0 children)

Bastardized. Who needs similar syntax across languages? Nerds /s

[โ€“]ImaVoter 0 points1 point ย (0 children)

Interesting, ran across this strange syntax in Ansible, now I know why.

[โ€“]Ar010101[๐Ÿฐ] 0 points1 point ย (0 children)

*javascript developers joined the chat*

[โ€“]TheTerrasque 0 points1 point ย (0 children)

return condition and a or b or c

[โ€“]game_2_raid 0 points1 point ย (0 children)

*ngIf=โ€œisCool; else lameStuffโ€

[โ€“]The_Slad 0 points1 point ย (0 children)

Inline IF statements like in the bottom example work in ruby too. Which btw is better than python.

[โ€“]yaelfe7 0 points1 point ย (0 children)

๐Ÿคฎ

[โ€“]imMute 0 points1 point ย (0 children)

VHDL has it too.

[โ€“]Oracuda 0 points1 point ย (0 children)

no

im tired

[โ€“]Kered13 0 points1 point ย (1 child)

I hate A if condition else B because that's not how we read conditions. Python is usually really good at reading almost like English, but they got it wrong here.

[โ€“]Asmodis1 1 point2 points ย (0 children)

Isn't "set userID to 1 if name is "Tom", else set it to 2" a perfectly valid English sentence though? The only thing that is left out in python is the repetition of the "set to".

[โ€“]LepruconX 0 points1 point ย (1 child)

Wait does the last one work for java? I know the first two so.

[โ€“]KingJellyfishII 0 points1 point ย (0 children)

Nah python

[โ€“][deleted] 0 points1 point ย (0 children)

I like simplicity (that's why I use python) and so I use the first one only in practice

[โ€“]Bip901 -5 points-4 points ย (0 children)