Alex Rivera | Logout

Hashable, immutable

Asked 2010-04-19T22:32:07.987
101

From a recent SO question (see Create a dictionary in python which is indexed by lists) I realized I probably had a wrong conception of the meaning of hashable and immutable objects in python.

  • What does hashable mean in practice?
  • What is the relation between hashable and immmutable?
  • Are there mutable objects that are hashable or immutable objects that are not hashable?
Edit
Report

1 Answer

8

Technically, hashable means that the class defines __hash__(). According to the docs:

__hash__() should return an integer. The only required property is that objects which compare equal have the same hash value; it is advised to somehow mix together (e.g. using exclusive or) the hash values for the components of the object that also play a part in comparison of objects.

I think that for the Python builtin types, all hashable types are also immutable.

It would be difficult but perhaps not impossible to have a mutable object that nonetheless defined __hash__().

answered 2010-04-19T22:41:03.010

Your Answer