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.
Variables with a value of None means they have no value at all. To compare it to False in the form of a metaphor, False would be like answering somebody by saying "No", where None would be like not answering them at all.
None vs False in Python In Python, None has nothing to do with the boolean value False. None in Python is actually an object implemented by NoneType class. False is a boolean object implemented by the bool class.
In this case, they are the same. None is a singleton object (there only ever exists one None ). is checks to see if the object is the same object, while == just checks if they are equivalent. But since there is only one None , they will always be the same, and is will return True.
This object is accessed through the built-in name None. It is used to signify the absence of a value in many situations, e.g., it is returned from functions that don't explicitly return anything. Its truth value is false.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With