Junior — Middle
Can you show an example of an object that cannot be hashed?
sobes.tech AI
Answer from AI
In Python, an object is not hashable if it is mutable and does not implement the __hash__ method. For example, lists (list) and dictionaries (dict) are not hashable because they are mutable.
Example:
my_list = [1, 2, 3]
try:
hash(my_list)
except TypeError as e:
print(f"Error: {e}") # Will print: unhashable type: 'list'
Therefore, objects of type list, dict, set cannot be used as keys in dictionaries or as elements of sets because they are not hashable.