Methods and Functions

Python hash()

Python as a programming language has evolved and developed in creating many modern-day high-level applications. Due to the ever-increasing demand for Python, the data being collected and being used needs to be protected. To resolve this issue, the Python hash module offers the hash() method that is used to encrypt the data into an unrecognizable value, free from any cyber-security issues. Let us first understand the concept of Hashing and how Python implements it by providing a built-in function.

Hashing Technique

Hashing is the process of converting one value into another based on a specified key or string of characters. They are commonly used in cryptography for authentication. There are numerous hashing functions, such as MD5, SHA-1, and so on. The most common application for hashing is the creation of hash tables. A hash table is a list that stores key and value pairs that may be accessed via its index. Hashing is used to develop high-speed, direct access data structures that store and access massive amounts of data quickly. Hash functions and algorithms are used to compute hash values.

Definition

  • The Python hash() function is used to return a hash value of an object if it has one.

Python hash()

The Python hash() built-in function returns an integer value for every object which is hashable. The hash method is used in programming to return integer values that can be used to compare dictionary keys using a dictionary look-up function. If an item has a hash value that never changes during its lifespan, it is hashable. Hence hash() function only works on immutable objects such as int, string, tuple, float, long, Unicode, bool. Mutable objects such as list, dict, set, bytearray are non-hashable.

Syntax

Internally, the hash() method invokes an object’s __hash__() method, which is set by default for any object.

Python hash() parameter

Hash() method accepts only a single parameter:

object – whose hash value is to be returned

Return value from hash()

If the object is immutable, the hash() function returns a hash value for it.

Example 1 – How hash() works in Python?

Example 1

                    

# Python program to illustrate hash() function
a = 100
b = 12.35
txt = 'Hello'

print('Hash value of 100 is:', hash(a))
print('Hash value of 12.35 is:', hash(b))
print('Hash value of Hello is:', hash(txt))

Output

                    

Hash value of 100 is: 100
Hash value of 12.35 is: 807045053224792076
Hash value of Hello is: -6456706318375158412

Note – Hash values can range from positive integers to negative integers as well.

Example 2 – hash() for immutable tuple objects?

The hash() function returns a hash value only in the case of immutable objects. Hence, this becomes one of the properties of Python hash(). It can be used as an indicator to check whether the objects are mutable or immutable in the program.

Example

                    

# hash() function on immutable objects like tuple
my_tuple = (1, 2, 3, 4, 5)
cars = ('BMW', 'Audi', 'Ford', 'Fiat')

print('Hash value of my_tuple is:', hash(my_tuple))
print('Hash value of cars tuple is:', hash(cars))

Output

                    

Hash value of my_tuple is: -5659871693760987716
Hash value of cars tuple is: -1476157316519184728

Example 3 – hash() for mutable objects?

Example

                    

my_list = [1, 'John', 2, 'Sam']
print('Hash value of list is:', hash(my_list))

Output

                    

Traceback (most recent call last):
File "", line 2, in 
TypeError: unhashable type: 'list'

Example

                    

my_dict = {1: 'Mumbai', 2: 'Delhi', 3: 'Chennai'}
print('Hash value of dict is:', hash(my_dict))

Output

                    

Traceback (most recent call last):
File "", line 2, in 
TypeError: unhashable type: 'dict'

How hash() works for custom objects?

We know that the hash() function internally calls __hash__() method, so any object will be able to override the custom hash value for an object. However, for proper hash implementation, __hash__() must always return an integer. Furthermore, both the __eq__() and __hash__() methods must be implemented. Python __eq__() is a method used to compare 2 objects by their values.

  • The cases for valid __hash__() override are listed below.
__eq__() __hash__() Description
Defined (by default) Defined (by default) Every object compares unequally.
(if mutable) Defined Should not be defined Requires Key’s hash value to be immutable
Not defined Should not be defined If __eq__() method not defined, __hash__() method also should not be defined
Defined Not defined __hash__() set to None

TypeError raised

Defined Retain from parent __hash__ = <Parent Class>.__hash__
Defined Doesn’t want to hash __hash__ = None

TypeError raised if we try to retrieve the hash

Example

                    

class Person:
    def __init__(self, name, salary):
        self.name = name
        self.salary = salary

    def __eq__(self, other):
        return self.name == other.name and self.salary == other.salary

    def __hash__(self):
        print('Hash is:')
        return hash((self.name, self.salary))

p1 = Person('John', 35000)
p2 = Person('Sam', 40000)

print(hash(p1))
print(hash(p2))

Output

                    

Hash is:
5241630111559171993
Hash is:
-6590424015832987399

Frequently Asked Questions

Q1. What is a Python hash?

Hashing is the process of converting one value into another based on a specified key or string of characters. The most common application for hashing is the creation of hash tables. Each value must have its unique hash, therefore you will obtain the same hash for the same value even though the object is not the same. Hash values are just numbers used to compare dictionary keys rapidly during a dictionary lookup. They are commonly used in cryptography for authentication. There are numerous hashing functions, such as MD5, SHA-1, and so on.

Q2. How does Python calculate hash?

Python provides a hash() built-in function to perform the hashing technique. The hash() function accepts a single object as a parameter and returns a hash which is an integer value. hash() function only works on immutable objects such as int, string, tuple, float, long, Unicode, bool. Mutable objects such as list, dict, set, bytearray are non-hashable.

Example

                    

# Python program to illustrate hash() function
int_val = 25
float_val = 43.232
string = 'Python'
my_tuple = (1, 2, 3, 4)

print('Hash value of int is:', hash(int_val))
print('Hash value of float is:', hash(float_val))
print('Hash value of string is:', hash(string))
print('Hash value of tuple is:', hash(my_tuple))

Output

                    

Hash value of int is: 25
Hash value of float is: 534955578137575467
Hash value of string is: -7203812434829324779
Hash value of tuple is: 590899387183067792

Q3. How do you hash a file in Python?

Hashing can also be done on a single file. In the example below, we will use the SHA-1 hashing algorithm. The SHA-1 digest is 160 bits long.

Because some files are too huge to fit in memory all at once, we do not feed the contents from the file all at once. The process will be more memory efficient if the file is divided into little bits.

Example

                    

# Python Program to find the hash value of a file
# import the hashlib module
import hashlib

def file(filename):
   # make a hash object
   h = hashlib.sha1()

   # open the file for reading in binary mode
   with open(filename,'rb') as file:

       # loop till the end of the file
       chunk = 0
       while chunk != b'':
           # read only 1024 bytes at a time
           chunk = file.read(1024)
           h.update(chunk)

   # return the hex representation of digest
   return h.hexdigest()

hash_val = file("SongTrack.mp3")
print(hash_val)

Output

                    

bfbb29e3e565efda0192c1be85069ebf06d7be12

Q4. What is a hash value?

A hash value is a fixed-length numeric value that uniquely identifies data. Large volumes of data are represented as much smaller numeric values through hash values. Hash values can be used to validate the integrity of data sent through insecure networks. To establish whether data was altered, the hash value of received data can be compared to the hash value of data as it was delivered.

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

Your email address will not be published. Required fields are marked *

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.