Python / Core Python Fundamentals Interview Questions
What is None in Python, and when should you use 'is' versus '=='?
None is Python's null value — a singleton object of type NoneType. It represents the absence of a value: default function returns, uninitialised optional variables, missing dict values. There is exactly one None object in any Python process.
The key distinction for comparisons:
==tests equality: do the two objects have the same value? It calls__eq__and can be overridden.istests identity: are the two names pointing to the exact same object in memory (sameid())?
# Correct: test for None with 'is' result = some_function() if result is None: print('No result returned') # Why not ==? class Weird: def __eq__(self, other): return True # lies â claims equality with everything w = Weird() print(w == None) # True â because __eq__ lies print(w is None) # False â identity check cannot be faked
PEP 8 says explicitly: use is and is not when comparing against None or the boolean singletons True/False. The is check is also marginally faster because it does not invoke any dunder method — it is a direct pointer comparison.
A related trap: CPython caches small integers (typically -5 to 256) and short strings, so a = 256; b = 256; a is b is True. But a = 1000; b = 1000; a is b may be False. Never rely on is for value comparisons — only use it for singletons like None.
More Related questions...