They are three different objects with different meanings, even though all are falsy in boolean context.
| Value | Type | Meaning | | --- | --- | --- | | None | NoneType singleton | Absence of a value / null | | False | bool | Boolean false | | 0 | int | Numeric zero |
Because they are all falsy, if not x cannot tell them apart. Use explicit checks.
print(None == False) # False
print(None == 0) # False
print(False == 0) # True (bool is a subclass of int!)
print(False is 0) # False
print(isinstance(False, int)) # True
def lookup(name):
data = {"empty": "", "zero": 0, "missing": None}
return data.get(name) # None if key absent OR value stored as None
# Bad: collapses many cases
value = lookup("zero")
if not value:
print("missing-or-empty-or-zero")
# Better
value = lookup("zero")
if value is None:
print("none")
elif value == 0:
print("zero")Interview tip: Mention that bool subclasses int (True == 1, False == 0) and always recommend is None for null checks.