Overview - Checking identity (is, is not)
What is it?
Checking identity means verifying if two variables point to the exact same object in memory. In Python, the operators 'is' and 'is not' are used for this purpose. Unlike equality (==), which checks if values are the same, identity checks if the objects themselves are the same. This is important in testing to ensure objects are not just equal but actually the same instance.
Why it matters
Without identity checks, tests might pass even if two variables are different objects with the same value. This can hide bugs where object uniqueness matters, such as caching, singleton patterns, or mutable objects. Identity checks help catch subtle errors that equality checks miss, ensuring more reliable and accurate tests.
Where it fits
Before learning identity checks, you should understand basic Python variables, objects, and equality (==) comparisons. After mastering identity checks, you can explore advanced testing concepts like mocking, fixtures, and test isolation in pytest.