Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 664
Virgiliu Avatar asked Oct 12 '10 12:10

Virgiliu


People also ask

What is the difference between None and false?

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.

Is None and false the same in Python?

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.

Should I use is None or == None?

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.

Is None True or false?

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.


1 Answers

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

like image 74
RichieHindle Avatar answered Oct 23 '22 03:10

RichieHindle