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.
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).
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 the 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
You must realize that None
, False
and True
are all singletons.
For example, if foo is not None
means that foo
has some value other than None
. This works the same as just having if foo
which is basically if foo == True
.
So, not None
and True
work the same way. Also, None
and False
work the same way.
>>> 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 important thing to realize and to be aware of when coding is that when comparing two things, None
needs is
, but True
and False
need ==
. Avoid if foo == None
and do only if foo is None
and avoid if foo != None
and do only if foo is not None
. In the case of if foo is not None
, simply do if foo
. In the case of if foo is None
, simply do if not foo
.
Note: True
is basically 1
and False
is basically 0
. In the old days of Python, we had only 1
for a value of true and we had 0
for a value of false. It's more understandable and human-friendly to say True
instead of 1
and more understandable and human-friendly to say False
instead of 0
.
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.
来源:https://stackoverflow.com/questions/3914667/false-or-none-vs-none-or-false