False or None vs. None or False

In [20]: print None or False
-------> print(None or False)
False


In [21]: print False or None
-------> print(False or None)
None

This behaviour confuses me. Could someone explain to me why is this happening like this? I expected them to both behave the same.

98362 次浏览

The expression x or y evaluates to x if x is true, or y if x is false.

Note that "true" and "false" in the above sentence are talking about "truthiness", not the fixed values True and False. Something that is "true" makes an if statement succeed; something that's "false" makes it fail. "false" values include False, None, 0 and [] (an empty list).

From a boolean point of view they both behave the same, both return a value that evaluates to false.

or just "reuses" the values that it is given, returning the left one if that was true and the right one otherwise.

Condition1 or Condition2

if Condition1 is False then evalute and return Condition2. None evalutes to False.

The or operator returns the value of its first operand, if that value is true in the Pythonic boolean sense (aka its "truthiness"), otherwise it returns the value of its second operand, whatever it happens to be. See the subsection titled Boolean operations in the section on Expressions in the current online documentation.

In both your examples, the first operand is considered false, so the value of the second one becomes the result of evaluating the expression.

A closely related topic: Python's or and and short-circuit. In a logical or operation, if any argument is true, then the whole thing will be true and nothing else needs to be evaluated; Python promptly returns that "true" value. If it finishes and nothing was true, it returns the last argument it handled, which will be a "false" value.

and is the opposite, if it sees any false values, it will promptly exit with that "false" value, or if it gets through it all, returns the final "true" value.

>>> 1 or 2 # first value TRUE, second value doesn't matter
1
>>> 1 and 2 # first value TRUE, second value might matter
2
>>> 0 or 0.0 # first value FALSE, second value might matter
0.0
>>> 0 and 0.0 # first value FALSE, second value doesn't matter
0

EXPLANATION

You must realize that True, False, and None are all singletons in Python, which means that there exist and can only exist one, single instance of a singleton object, hence the name singleton. Also, you can't modify a singleton object because its state is set in stone, if I may use that phrase.

Now, let me explain how those Python singletons are meant to be used.

Let's have a Python object named foo that has a value None, then if foo is not None is saying that foo has a value other than None. This works the same as saying if foo, which is basically if foo == True.

So, not None and True work the same way, as well as None and False.

>>> foo = not None
>>> bool(foo)
True
>>> foo = 5  # Giving an arbitrary value here
>>> bool(foo)
True


>>> foo = None
>>> bool(foo)
False
>>> foo = 5  # Giving an arbitrary value here
>>> bool(foo)
True

The critical thing to realize and be aware of when coding is that when comparing two objects, None needs is, but True and False need ==. Avoid using if foo == None, only use if foo is None. Also, avoid using if foo != None and only use if foo is not None.

WARNING

If you are using if foo or if not foo when the value of foo happens to be None, beware of potential bugs in your code. So, don't check for a potential None value in conditional statements this way. Be on the safe side by checking for it as explained above, i.e., if foo is None or if foo is not None. It is very important to follow best practices shared by Python experts.

REMINDER

True is a 1 and False is a 0.

In the old days of Python, we only had the integer 1 to represent a truthy value and we had the integer 0 to represent a falsy value. However, it is more understandable and human-friendly to say True instead of 1 and it is more understandable and human-friendly to say False instead of 0.