Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why sets,dicts,list are unhashable in python

Tags:

python

hash

set

What exactly is meant by unhashable?

>>> a={1,2,3}
>>> b={4,5,6}
>>> set([a,b])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'set'
>>>

Can any one tell what the error is exactly? Also can i add set into another set in python?

like image 951
dharmista Avatar asked Oct 16 '25 16:10

dharmista


1 Answers

Objects that doesn't have the __hash__() attribute called unhashable. Python documentation has described the reason very well:

If a class defines mutable objects and implements an __eq__() method, it should not implement __hash__(), since the implementation of hashable collections requires that a key’s hash value is immutable (if the object’s hash value changes, it will be in the wrong hash bucket).

like image 129
Mazdak Avatar answered Oct 18 '25 06:10

Mazdak