77. Custom Hashing for Objects
1. Basic Custom Hashing
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def __eq__(self, other):
return (self.name, self.age) == (other.name, other.age)
def __hash__(self):
return hash((self.name, self.age))
# Using in set
person1 = Person("Alice", 30)
person2 = Person("Alice", 30)
people = {person1, person2}
print(people) # Only one entry, because Person objects are hashable and equal2. Hashing with Multiple Attributes
3. Custom Hash for Mutable Attributes
4. Handling Hash Collisions
5. Using Only Immutable Attributes for Hashing
6. Custom Hash with Complex Objects
7. Handling Non-Hashable Attributes
8. Custom Hashing with String Representation
9. Custom Hashing with Frozen Set
10. Debugging Hash and Equality
Last updated