Python hash() method

17 Sep, 2021
Python hash() function is a built-in function and returns the hash value of an object if it has one. The hash value is an integer which is used to quickly compare dictionary keys while looking at a dictionary.
Syntax : hash(obj)
Parameters :  obj : The object which we need to convert into hash.
Returns : Returns the hashed value if possible. 
Objects hashed using hash() are irreversible, leading to loss of information.
hash() returns hashed value only for immutable objects, hence can be used as an indicator to check for mutable/immutable objects.
Python3
# Python 3 code to demonstrate# working of hash() # initializing objectsint_val = 4str_val = 'GeeksforGeeks'flt_val = 24.56 # Printing the hash values.# Notice Integer value doesn't change# You'l have answer later in article.print("The integer hash value is : " + str(hash(int_val)))print("The string hash value is : " + str(hash(str_val)))print("The float hash value is : " + str(hash(flt_val)))
Output: 
The integer hash value is : 4
The string hash value is : -5570917502994512005
The float hash value is : 1291272085159665688
Python3
# Python 3 code to demonstrate# property of hash() # initializing objects# tuple are immutabletuple_val = (1, 2, 3, 4, 5) # list are mutablelist_val = [1, 2, 3, 4, 5] # Printing the hash values.# Notice exception when trying# to convert mutable objectprint("The tuple hash value is : " + str(hash(tuple_val)))print("The list hash value is : " + str(hash(list_val)))
Output: 
The tuple hash value is : 8315274433719620810
Exceptions : 
Traceback (most recent call last):
  File "/home/eb7e39084e3d151114ce5ed3e43babb8.py", line 15, in 
    print ("The list hash value is : " + str(hash(list_val)))
TypeError: unhashable type: 'list'
Python3
# hash() for immutable tuple objectvar = ('G','E','E','K') print(hash(var))
Output:
5434435027328283763
hash() method used by on immutable object, if we use this on a mutable object like list, set, dictionaries then it will generate an error.
Python3
l = [1, 2, 3, 4]print(hash(l))
Output:
TypeError: unhashable type: 'list'
Here we will override the __hash()__ methods to call the hash(), and __eq__() method will check the equality of the two custom objects.
Python3
class Emp:    def __init__(self, emp_name, id):        self.emp_name = emp_name        self.id = id     def __eq__(self, other):               # Equality Comparison between two objects        return self.emp_name == other.emp_name and self.id == other.id     def __hash__(self):               # hash(custom_object)        return hash((self.emp_name, self.id)) emp = Emp('Ragav', 12)print("The hash is: %d" % hash(emp)) # We'll check if two objects with the same# attribute values have the same hashemp_copy = Emp('Ragav', 12)print("The hash is: %d" % hash(emp_copy))
Output:
The hash is: -674930604243231063
The hash is: -674930604243231063
WaahModiJiWaah
kumar_satyam
Python-Built-in-functions
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
Different ways to create Pandas Dataframe
Enumerate() in Python
Python String | replace()
How to Install PIP on Windows ?
*args and **kwargs in Python
Python Classes and Objects
Convert integer to string in Python
Python | os.path.join() method
Python OOPs Concepts