Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "hashable" mean in Python?

Tags:

python

I tried searching internet but could not find the meaning of hashable.

When they say objects are hashable or hashable objects what does it mean?

like image 568
user1755071 Avatar asked Oct 08 '22 06:10

user1755071


People also ask

What are hashable types Python?

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

What is meant by hashable objects?

An object is said to be hashable if it has a hash value that remains the same during its lifetime.

Does hashable mean immutable?

Hashable. 00:00 Immutable objects are a type of object that cannot be modified after they were created. Hashable objects, on the other hand, are a type of object that you can call hash() on.

Is a function hashable in Python?

A function is hashable because it is a normal, builtin, non mutable object. From the Python Manual: An object is hashable if it has a hash value which never changes during its lifetime (it needs a __hash__() method), and can be compared to other objects (it needs an __eq__() or __cmp__() method).


Video Answer


1 Answers

From the Python glossary:

An object is hashable if it has a hash value which never changes during its lifetime (it needs a __hash__() method), and can be compared to other objects (it needs an __eq__() or __cmp__() method). Hashable objects which compare equal must have the same hash value.

Hashability makes an object usable as a dictionary key and a set member, because these data structures use the hash value internally.

All of Python’s immutable built-in objects are hashable, while no mutable containers (such as lists or dictionaries) are. Objects which are instances of user-defined classes are hashable by default; they all compare unequal, and their hash value is their id().

like image 257
NPE Avatar answered Oct 13 '22 00:10

NPE