Equality, Identity, and Hashing
CONCEPTS:Python Equality and Hashing
Question Variations
- "What is the difference between `is` and `==`?"
- "Why does defining `__eq__` affect hashability?"
- "Can a mutable object be a dictionary key?"
- "What must be true for two equal objects' hashes?"
Why This Is Asked
This question checks whether you understand how Python collections determine membership and key lookup. It is important when designing value objects because violating the equality/hash contract causes subtle dictionary and set bugs.
Key Concepts
iscompares object identity;==calls value equality through__eq__.- Hash-based collections use hashes to narrow lookup and equality to confirm matches.
- Equal hashable objects must have equal hashes.
- Mutable objects should not change the fields used by their hash while stored in a set or dictionary.
Question Variations
- “What is the difference between
isand==?” - “Why does defining
__eq__affect hashability?” - “Can a mutable object be a dictionary key?”
- “What must be true for two equal objects’ hashes?”