Hashability in Practice
Real-World Hashability Scenarios
1. Dictionary Key Management
def unique_elements(items):
return list(dict.fromkeys(items))
## Example usage
data = [1, 2, 2, 3, 4, 4, 5]
unique = unique_elements(data)
print(unique) ## [1, 2, 3, 4, 5]
2. Set Operations
def remove_duplicates(hashable_collection):
return set(hashable_collection)
## Demonstration
names = ['Alice', 'Bob', 'Charlie', 'Alice', 'Bob']
unique_names = remove_duplicates(names)
print(unique_names) ## {'Alice', 'Bob', 'Charlie'}
Handling Unhashable Types
Converting Unhashable to Hashable
def make_hashable(lst):
return tuple(lst)
## Example
unhashable_list = [1, 2, 3]
hashable_tuple = make_hashable(unhashable_list)
print(hash(hashable_tuple)) ## Successful hash
graph TD
A[Hashability Check] --> B{Is Object Hashable?}
B -->|Yes| C[Fast Lookup]
B -->|No| D[Conversion/Transformation Needed]
Operation |
Hashable |
Unhashable |
Performance Impact |
Dictionary Lookup |
O(1) |
Requires Conversion |
High |
Set Operations |
Instant |
Requires Transformation |
Moderate |
Caching |
Efficient |
Challenging |
Significant |
Advanced Hashability Techniques
Custom Hashable Class
class HashableRecord:
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
record1 = HashableRecord('John', 30)
record2 = HashableRecord('John', 30)
record_set = {record1, record2}
print(len(record_set)) ## 1
LabEx Optimization Strategies
- Prefer immutable types for hash-based collections
- Implement
__hash__()
and __eq__()
carefully
- Convert complex objects to hashable representations
def transform_to_hashable(data):
try:
hash(data)
return data
except TypeError:
return str(data)
## Example
mixed_data = [1, 'hello', [1, 2], {'key': 'value'}]
hashable_data = [transform_to_hashable(item) for item in mixed_data]
print(hashable_data)
Key Takeaways
- Understand the importance of hashability
- Know how to check and convert types
- Implement custom hashable classes when needed
- Consider performance implications