Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is a tuple containing an unhashable type unhashable?

Tags:

python

For example, the tuple (1,[0,1,2]). I understand why from a design perspective; if the tuple were still hashable, then it would be trivial to make any unhashable type hashable by wrapping it in a tuple, which breaks the correct behavior of hashability, since you can change the value of the object without changing the hash value of the tuple. But if the tuple is not hashable, then I don't understand what makes an object hashable -- I thought it simply had to have __hash__(self) implemented, which tuple does.

Based on other answers I've looked at, and from testing examples, it seems that such an object is not hashable. It seems like sensible behavior would be for tuple.__hash__() to call __hash__ for its component objects, but I don't understand how that would work from an implementation perspective, e.g. I don't know how a dictionary recognizes it as an unhashable type when it is still type tuple and tuple still defines __hash__.

like image 553
colopop Avatar asked Oct 17 '17 04:10

colopop


People also ask

What does Unhashable type mean Python?

What are Unhashable Type Errors in Python? Unhashable type errors appear in a Python program when a data type that is not hashable is used in code that requires hashable data. An example of this is using an element in a set or a list as the key of a dictionary.

What does it mean by Unhashable type?

TypeError: unhashable type: 'list' usually means that you are trying to use a list as an hash argument. This means that when you try to hash an unhashable object it will result an error. For ex. when you use a list as a key in the dictionary , this cannot be done because lists can't be hashed.

Why is set Unhashable in Python?

The Python "TypeError: unhashable type: 'set'" occurs when we use a set as a key in a dictionary or an element in another set . To solve the error, use a frozenset instead, because set objects are mutable and unhashable.

Why list is Unhashable and tuple is hashable?

Because a list is mutable, while a tuple is not. When you store the hash of a value in, for example, a dict, if the object changes, the stored hash value won't find out, so it will remain the same.

What does unhashable type list mean in Python?

The Python TypeError: unhashable type: 'list' usually means that a list is being used as a hash argument. This error occurs when trying to hash a list, which is an unhashable object. For example, using a list as a key in a Python dictionary will cause this error since dictionaries only accept hashable data types as a key.

What is a unhashable data type?

Unhashable: For this data-type, the value remains constant throughout. For this data-type, the value is not constant and change. Some data types that fall under this category are- int, float, tuple, bool, string, bytes. Some data types that fall under this category are- list, set, dict, bytearray.

What does unhashable type ‘slice’ mean?

Unhashable Type ‘Slice’ Error in Python The error unhashable type: ‘slice’ occurs if you try to use the slice operator with a data type that doesn’t support it. For example, you can use the slice operator to get a slice of a Python list. But what happens if you apply the slice operator to a dictionary?

What is hashable and unhashable data in Python?

Specific Python data types require hashable data, for example the items of a set have to be hashable or the keys of a Python dictionary have to be hashable. If unhashable data is used where hashable data is required the unhashable type error is raised by the Python interpreter.


1 Answers

tuple implements its own hash by computing and combining the hashes of the values it contains. When hashing one of those values fails, it lets the resulting exception propagate unimpeded.

Being unhashable just means calling hash() on you triggers a TypeError; one way to do that is to not define a __hash__ method, but it works equally well if, in the course of your __hash__ method you raise a TypeError (or any other error really) by some other means.

Basically, tuple is a hashable type (isinstance((), collections.abc.Hashable) is true, as is isinstance(([],), collections.abc.Hashable) because it's a type level check for the existence of __hash__), but if it stores unhashable types, any attempt to compute the hash will raise an exception at time of use, so it behaves like an unhashable type in that scenario.

like image 141
ShadowRanger Avatar answered Oct 27 '22 06:10

ShadowRanger