33. Python's Built-in Hash Functions
1. Basic Hashable Object
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def __hash__(self):
return hash((self.name, self.age))
def __eq__(self, other):
return (self.name, self.age) == (other.name, other.age)
# Usage
person1 = Person("Alice", 30)
person2 = Person("Alice", 30)
person3 = Person("Bob", 25)
# Using the objects in a set
people_set = {person1, person2, person3}
print(people_set) # Outputs: {<__main__.Person object at 0x...>, <__main__.Person object at 0x...>}2. Hashable Object with Tuple
3. Hashable Object with Immutable Attributes
4. Custom Hashable Class Using __slots__
__slots__5. Hashable Object with Custom __repr__
__repr__6. Hashable Object with Custom Attributes
7. Hashable Class Using frozenset
frozenset8. Hashable Object with Default Dictionary Behavior
9. Hashable Object with __eq__ and __ne__
__eq__ and __ne__10. Hashable Object with Multiple Attributes
Last updated