- What's the difference between immutable and hash-able ?
- What does this mean "An object is hashable if it has a hash value which never changes during its lifetime?"
- Is a tuple immutable and hashable or only immutable ? why ?
-
Possible duplicate of [List unhashable, but tuple hashable?](https://stackoverflow.com/questions/37136878/list-unhashable-but-tuple-hashable) – Triggernometry May 15 '19 at 17:30
-
It's not about examples it's about concept i just to understanding the differences – N.Elsayed May 15 '19 at 17:32
3 Answers
Hashing is the application of some hashing algorithm to a large piece of data; typically in a means to compress it into a much smaller value that is searchable in a hash look-up table. Some examples of hashing include MD5 and SHA-2.
Some hashes are considered "defunct" when they produce a collision - meaning, that two very different pieces of data result in the same "compressed" string or integer. MD5 is defunct because there are collisions, but most SHA-2 variations are not.
Immutability is the process of ensuring that something does not change. For example, imagine a static binary of a C-program. You don't want its contents to change once you publish it to the world wide web, or to an end user, so you want it to be immutable.
This is relevant for hashing. Once you "hash" an object, you don't want its contents to change, or else you would result in getting a different hash. If you change contents and the hash does not change, then you have a collision!
A tuple is just a data structure and should be both immutable and hashable. If you are having trouble doing so, it might be a code issue.

- 6,857
- 11
- 46
- 99
Immutable means that the object, the top-level container for the item, cannot be changed. Note that this applies only to the top level; it may contain references to sub-objects that are mutable.
Hashable has a functional definition: Python's built-in
hash
function returns a value. This generally means that the object's closure (following all references to their leaf-node values) consists of immutable objects.Your premise is incorrect: a tuple can contain mutable items. Any such reference renders the tuple un-hashable.
For instance:
>>> b = [7]
>>> a = (b, 5)
>>> a
([7], 5)
>>> type(a)
<class 'tuple'>
>>> set(a)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'
>>> b[0] = 'b'
>>> a
(['b'], 5)
b
is a mutable list. The reference to b
can be held in the tuple a
. We can change the value of b[0]
(not it's object handle), and the display value of a
changes. However, we cannot make a set including a
, because the mutability of b
renders a
unhashable.
Continuing the example:
>>> b = False
>>> a
(['b'], 5)
>>> b = [14]
>>> a
(['b'], 5)
a
is immutable. Thus, when we change b
, only b
gets the reference to the new object. a
retains the original object handle, still pointing to ['b']
.

- 76,765
- 14
- 60
- 81
- Hashable means that an object can be hashed (a value will be produced when used with the
hash()
function, while immutable means that an object cannot be "mutated" or changed. They tend to go together, as mutable arguments would be terrible dictionary keys (they would change their hash every time they are changed) - If an item has a never-changing value associated with
hash(item)
, it is hashable - Tuples are both immutable and hashable

- 8,529
- 8
- 37
- 63