Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Situation

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

Question

The last example seems to be easier to read, but is it considered pythonic?

Neither pep8 (the program) nor pylint do complain.

like image 662
Sebastian Mach Avatar asked May 01 '14 07:05

Sebastian Mach


People also ask

What is right sided binding in Python?

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.

Are Python expressions evaluated left to right?

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.

How many variables can come on left hand side of assignment statement in Python?

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.


1 Answers

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

like image 62
thefourtheye Avatar answered Nov 07 '22 06:11

thefourtheye