Is ‘or’ used on the right-hand-side of an assignment pythonic?


(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
    list = default_list

You can shorten this to:

default_list = ...
custom_list = ...
list = custom_list if custom_list else default_list

Now, as per

The expression x or y first evaluates x; 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.

Best answer

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 and True, but rather return the last evaluated argument. This is sometimes useful, e.g., if _s_ is a string that should be replaced by a default value if it is empty, the expression _s 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 with a and b or
c, but there’s a flaw: if b is zero (or empty, or None — 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’.