Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hashable, immutable

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?
like image 443
joaquin Avatar asked Apr 19 '10 22:04

joaquin


People also ask

Why are immutable objects hashable?

We could say that all immutable objects are hashable, because if the hash changes during the object's lifetime, then it means that the object mutated. But not quite. Consider a tuple which has a list (mutable). Some say tuple is immutable, but at the same time it is somewhat not hashable (throws).

What does it mean for something to be hashable?

An object is hashable if it has a hash value that does not change during its entire lifetime. Python has a built-in hash method ( __hash__() ) that can be compared to other objects. For comparing it needs __eq__() or __cmp__() method and if the hashable objects are equal then they have the same hash value.

What is hashable and Unhashable?

In particular, all the primitive data types (strings, integers, floats, complex, boolean, bytes), ranges, frozensets, functions, and classes, both built-in and user-defined, are always hashable, while lists, dictionaries, sets, and bytearrays are unhashable.

What types are hashable in Python?

Hashable data types: int , float , str , tuple , and NoneType .


2 Answers

Hashing is the process of converting some large amount of data into a much smaller amount (typically a single integer) in a repeatable way so that it can be looked up in a table in constant-time (O(1)), which is important for high-performance algorithms and data structures.

Immutability is the idea that an object will not change in some important way after it has been created, especially in any way that might change the hash value of that object.

The two ideas are related because objects which are used as hash keys must typically be immutable so their hash value doesn't change. If it was allowed to change then the location of that object in a data structure such as a hashtable would change and then the whole purpose of hashing for efficiency is defeated.

To really grasp the idea you should try to implement your own hashtable in a language like C/C++, or read the Java implementation of the HashMap class.

like image 162
maerics Avatar answered Oct 13 '22 12:10

maerics


  • Are there mutable objects that are hashable or immutable objects that are not hashable?

In Python, tuple is immutable, but it is hashable only if all its elements are hashable.

>>> tt = (1, 2, (30, 40)) >>> hash(tt) 8027212646858338501 >>> tl = (1, 2, [30, 40]) >>> hash(tl) TypeError: unhashable type: 'list' 

Hashable Types

  • The atomic immutable types are all hashable, such as str, bytes, numeric types
  • A frozen set is always hashable(its elements must be hashable by definition)
  • A tuple is hashable only if all its elements are hashable
  • User-defined types are hashable by default because their hash value is their id()
like image 36
Yihe Avatar answered Oct 13 '22 10:10

Yihe