Is there any difference between foo is None and foo None?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Yes, there is a real difference between foo is None and foo == None. The first checks identity, while the second checks equality.
In Python, None is a singleton, so the idiomatic and correct test is is None. This is not only about style. It avoids custom equality behavior and communicates your intent precisely.
Identity and Equality Are Different Operations
The is operator checks whether two names refer to the exact same object in memory. The == operator checks whether two objects should be considered equal according to their equality rules.
For ordinary values, equality is usually what you want:
Those two lists have the same contents, so they are equal, but they are not the same object.
For None, identity is the right question because there is only one None object:
Why == None Can Be Wrong
Equality can be overloaded through __eq__. That means an object can claim to be equal to None even when it is not actually None.
This example is intentionally odd, but it demonstrates the risk clearly. == None can run arbitrary application code. is None cannot.
That is the real reason Python style guides recommend identity checks for None.
The Idiomatic Form
PEP 8 recommends:
- '
if value is None:' - '
if value is not None:'
That style is both explicit and safe:
The intent is obvious: this branch is about the absence of a value, not about general equality.
Linters reinforce this rule because it prevents subtle bugs and makes code reviews easier. When another Python developer sees is None, they immediately know the code is testing for the singleton object itself rather than relying on a custom equality definition.
Do Not Confuse None With Falsy Values
Another common mistake is replacing a None check with a truthiness check.
These mean different things. not value matches empty strings, empty containers, 0, False, and None. Use it only when you intentionally want that broader behavior.
If you specifically need to know whether a variable is missing rather than merely falsy, use is None.
This shows up often in function arguments. An empty list may be a valid value, while None may mean "the caller did not supply anything." Those two cases should not be collapsed into the same condition accidentally.
Common Pitfalls
- Writing
== Noneout of habit from other languages. - Using
if not valuewhen the code should only treatNoneas missing. - Using
isfor ordinary value equality checks where==is the correct operator. - Assuming
== Noneis harmless just because it often works with built-in types.
Summary
- '
is Nonechecks identity and is the correct way to test forNone.' - '
== Nonechecks equality and can trigger custom__eq__logic.' - '
Noneis a singleton, which is why identity is the right semantic check.' - Do not replace a
Nonecheck with a generic falsy check unless that is truly what you want. - Use
is not Nonefor the inverse condition.
Related reading
- Is there any difference between using ABC vs ABCMeta?
- Is there any pythonic way to combine two dicts adding values for keys that appear in both?
- Is there any way to do HTTP PUT request in Python?
- Is there any way to list queues in rabbitmq via pika?
- Is there any way to show the dependency trees for pip packages?
- Is there anyway to know the progress in sklearn GridSearch
- Is there shorthand for returning a default value if None in Python?
- Is there something like RStudio for Python?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.