1 if A else 2 if B else 3 translates to this:
def myexpr(A, B):
if A:
return 1
else:
if B:
return 2
else:
return 3
Your ternary expression can be interpreted with parentheses as follows:
(
(1 if A) else (
(2 if B) else 3
)
)
Answer from inspectorG4dget on Stack Overflow1 if A else 2 if B else 3 translates to this:
def myexpr(A, B):
if A:
return 1
else:
if B:
return 2
else:
return 3
Your ternary expression can be interpreted with parentheses as follows:
(
(1 if A) else (
(2 if B) else 3
)
)
Could someone please explain why this is executed in this order, and possibly suggest some material that gives an intuition about why this is used/preferred?
I'm trying to answer the "intuition" part of your question by solving a simple but more general problem.
'''
+-----------------------------------------------------------------------------------+
| Problem: |
+-----------------------------------------------------------------------------------+
| Convert a |
| nested if-else block into |
| a single line of code by using Pythons ternary expression. |
| In simple terms convert: |
| |
| 1.f_nested_if_else(*args) ( which uses |
| ```````````````````` nested if-else's) |
| | |
| +--->to its equivalent---+ |
| | |
| V |
| 2.f_nested_ternary(*args) ( which uses |
| ``````````````````` nested ternary expression) |
+-----------------------------------------------------------------------------------+
'''
'''
Note:
C:Conditions (C, C1, C2)
E:Expressions (E11, E12, E21, E22)
Let all Conditions, Expressions be some mathematical function of args passed to the function
'''
#-----------------------------------------------------------------------------------+
#| 1. | Using nested if-else |
#-----------------------------------------------------------------------------------+
def f_nested_if_else(*args):
if(C):
if(C1):
return E11
else:
return E12
else:
if(C2):
return E21
else:
return E22
#-----------------------------------------------------------------------------------+
#| 2. | Using nested ternary expression |
#-----------------------------------------------------------------------------------+
def f_nested_ternary(*args):
return ( (E11) if(C1)else (E12) ) if(C)else ( (E21) if(C2)else (E22) )
#-----------------------------------------------------------------------------------+
#-----------------------------------------------------------------------------------|
#-----------------------------------------------------------------------------------|
#-----------------------------------------------------------------------------------|
#-----------------------------------------------------------------------------------+
Here is a visualization of why f_nested_if_else() and f_nested_ternary() are equivalent.
# +-----------------------------------------------------------------------------+
# | Visualization: |
# +-----------------------------------------------------------------------------+
# | Visualize the ternary expression like a binary tree : |
# | -Starting from the root and moving down to the leaves. |
# | -All the internal nodes being conditions. |
# | -All the leaves being expressions. |
# +-----------------------------------------------------------------------------+
_________________
|f_nested_ternary|
``````````````````
( (E11) if(C1)else (E12) ) if(C)else ( (E21) if(C2)else (E22) )
| | | | | | |
| | | | | | |
V V V V V V V
Level-1| +----------------(C)-----------------+
-------- True/ __________________ \False
V |f_nested_if_else| V
Level-2| +----(C1)----+ `````````````````` +----(C2)----+
-------- True/ \False True/ \False
V V V V
Level-3| ( (E11) (E12) ) ( (E21) (E22) )
------------------------------------------------------------------------------------+
Hope this visualization gave you an intuition of how nested ternary expressions are evaluated :P
Videos
Your first example (the horrid one-liner) is nested too. Horizontally nested. Your second example is vertically nested. They're both nested.
So which is better? The second one! Why? Because "sparse is better than dense" breaks the tie.
It's easy when you're Tim Peters - LOL ;-)
"Flat is better than nested" is about module organization and perhaps data structures, not your source code. The standard library, for example, mostly exists as top-level modules with very little nesting.
Don't nest the ternary operator, or even use it at all if you can avoid it. Complex is better than complicated. :)
Yes, it was added in version 2.5. The expression syntax is:
a if condition else b
First condition is evaluated, then exactly one of either a or b is evaluated and returned based on the Boolean value of condition. If condition evaluates to True, then a is evaluated and returned but b is ignored, or else when b is evaluated and returned but a is ignored.
This allows short-circuiting because when condition is true only a is evaluated and b is not evaluated at all, but when condition is false only b is evaluated and a is not evaluated at all.
For example:
>>> 'true' if True else 'false'
'true'
>>> 'true' if False else 'false'
'false'
Note that conditionals are an expression, not a statement. This means you can't use statements such as pass, or assignments with = (or "augmented" assignments like +=), within a conditional expression:
>>> pass if False else pass
File "<stdin>", line 1
pass if False else pass
^
SyntaxError: invalid syntax
>>> # Python parses this as `x = (1 if False else y) = 2`
>>> # The `(1 if False else x)` part is actually valid, but
>>> # it can't be on the left-hand side of `=`.
>>> x = 1 if False else y = 2
File "<stdin>", line 1
SyntaxError: cannot assign to conditional expression
>>> # If we parenthesize it instead...
>>> (x = 1) if False else (y = 2)
File "<stdin>", line 1
(x = 1) if False else (y = 2)
^
SyntaxError: invalid syntax
(In 3.8 and above, the := "walrus" operator allows simple assignment of values as an expression, which is then compatible with this syntax. But please don't write code like that; it will quickly become very difficult to understand.)
Similarly, because it is an expression, the else part is mandatory:
# Invalid syntax: we didn't specify what the value should be if the
# condition isn't met. It doesn't matter if we can verify that
# ahead of time.
a if True
You can, however, use conditional expressions to assign a variable like so:
x = a if True else b
Or for example to return a value:
# Of course we should just use the standard library `max`;
# this is just for demonstration purposes.
def my_max(a, b):
return a if a > b else b
Think of the conditional expression as switching between two values. We can use it when we are in a 'one value or another' situation, where we will do the same thing with the result, regardless of whether the condition is met. We use the expression to compute the value, and then do something with it. If you need to do something different depending on the condition, then use a normal if statement instead.
Keep in mind that it's frowned upon by some Pythonistas for several reasons:
- The order of the arguments is different from those of the classic
condition ? a : bternary operator from many other languages (such as C, C++, Go, Perl, Ruby, Java, JavaScript, etc.), which may lead to bugs when people unfamiliar with Python's "surprising" behaviour use it (they may reverse the argument order). - Some find it "unwieldy", since it goes contrary to the normal flow of thought (thinking of the condition first and then the effects).
- Stylistic reasons. (Although the 'inline
if' can be really useful, and make your script more concise, it really does complicate your code)
If you're having trouble remembering the order, then remember that when read aloud, you (almost) say what you mean. For example, x = 4 if b > 8 else 9 is read aloud as x will be 4 if b is greater than 8 otherwise 9.
Official documentation:
- Conditional expressions
- Is there an equivalent of Cโs โ?:โ ternary operator?
You can index into a tuple:
(falseValue, trueValue)[test]
test needs to return True or False.
It might be safer to always implement it as:
(falseValue, trueValue)[test == True]
or you can use the built-in bool() to assure a Boolean value:
(falseValue, trueValue)[bool(<expression>)]