(Note: The following situation is just exemplary. This question applys to anything that can evaluate to bool)
A default list should be used if the user does not provide a custom list:
default_list = ...
custom_list = ...
if custom_list:
list = custom_list
else:
list = default_list
You can shorten this to:
default_list = ...
custom_list = ...
list = custom_list if custom_list else default_list
Now, as per https://docs.python.org/2.7/reference/expressions.html#or ...
The expression
x or y
first evaluatesx
; if x is true, its value is returned; otherwise, y is evaluated and the resulting value is returned.
..., or
does not return a boolean, but rather the first value whose boolean conversion is not false. Therefore, the following would be valid code:
list = custom_list or default_list
This is similar to the C# Null Coalescing Operator, except it should be recoined in Python as False Coalescing Operator, which returns the first non-false argument.
The last example seems to be easier to read, but is it considered pythonic?
Neither pep8 (the program) nor pylint do complain.
Right-sided binding means that the following expression: 1 ** 2 ** 3 will be evaluated: from left to right. in random order. from right to left.
Order of Evaluation In Python, the left operand is always evaluated before the right operand. That also applies to function arguments. Python uses short circuiting when evaluating expressions involving the and or or operators.
You can assign to more than three variables. It is also possible to assign to different types. If there is one variable on the left side, it is assigned as a tuple.
That is perfectly valid and you can use that. Even the documentation of or
has an example for the same.
Note that neither and nor or restrict the value and type they return to
False
andTrue
, but rather return the last evaluated argument. This is sometimes useful, e.g., ifs
is a string that should be replaced by a default value if it is empty, the expressions or 'foo'
yields the desired value.
However, the or
method has a limitation. If you want to purposefully allow a Non-Truthy value, then it is not possible with that.
Let us say you want to allow an empty list
my_list = [] or default_list
will always give default_list
. For example,
print [] or [1, 2, 3]
# [1, 2, 3]
But with conditional expression we can handle it like this
custom_list if isinstance(custom_list, list) else default_list
Dusting off the older documents, quoting the BDFL's FAQ,
4.16. Q. Is there an equivalent of C's "?:" ternary operator?
A. Not directly. In many cases you can mimic
a?b:c
witha and b or c
, but there's a flaw: ifb
is zero (or empty, orNone
-- anything that tests false) then c will be selected instead. In many cases you can prove by looking at the code that this can't happen (e.g. because b is a constant or has a type that can never be false), but in general this can be a problem.Steve Majewski (or was it Tim Peters?) suggested the following solution:
(a and [b] or [c])[0]
. Because[b]
is a singleton list it is never false, so the wrong path is never taken; then applying[0]
to the whole thing gets the b or c that you really wanted. Ugly, but it gets you there in the rare cases where it is really inconvenient to rewrite your code using 'if'.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With