This is perfectly true, and you can use it. Even for the or documentation , an example is provided for this.
Note that neither one, nor the constraint or type of value returns to False and True , but rather returns the last evaluated argument. This is sometimes useful, for example, if s is a string that should be replaced with a default value, if it is empty, the expression s or 'foo' gives the desired value.
However, the or method has a limitation. If you want to specifically enable the Non-Truthy value, then this is not possible.
Suppose you want to allow an empty list
my_list = [] or default_list
will always give default_list . For instance,
print [] or [1, 2, 3]
But with a conditional expression, we can handle it this way:
custom_list if isinstance(custom_list, list) else default_list
Recycling old documents, quoting the BDFL FAQ ,
4.16. Q. Is there an equivalent C "?:" Ternary operator?
a. Not directly. In many cases, you can simulate a?b:c using a and b or c , but there is a drawback: if b is zero (or empty or None - anything that checks false), then c will be selected. In many cases, you can prove by looking at the code that this cannot happen (for example, 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?) Proposed the following Solution: (a and [b] or [c])[0] . Since [b] is a singleton list, it is never false, so the wrong path is never accepted; then applying [0] to it all gets the b or c that you really wanted. Ugly, but it gets there on rare occasions when it is really inconvenient to rewrite your code using "if".