Identity vs Equality
Pronunciationeye-DEN-ti-tee vee-ess ee-KWOL-i-tee
Definition
Equality asks whether two values are the same; identity asks whether two names point to the very same object. In Python, == checks equality and is checks identity.
Where you hear it
In Python code reviews, in interviews, and when two lists look equal but behave differently.
Examples
Two lists with the same items are equal, but they are not the same object.
Use
is Nonerather than== Noneto check for the absence of a value.The two lists are equal but not the same object, so the is check returns False.
Common mistake
Using is to compare numbers or strings. It may seem to work for small values, but only == is reliable for values.
Don't confuse with
A copy, which has equal contents but a different identity.
Say it at work
- In a meeting or chat
Compare with
is None, not== None. - In an email or ticket
They are equal but not identical, so changing one does not change the other.
Keep exploring
More in Programming Fundamentals