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'.