We Offer 100% Job Guarantee Courses (Any Degree / Diploma Candidates / Year GAP / Non-IT / Any Passed Outs). Placement Records
Hire Talent (HR):+91-9707 240 250

General

Hash in Python

Hash in Python

Hash in Python

Today’s digital world provides an application for every requirement that you can think of. We are living in an era where technologies like Artificial Intelligence, Machine Learning, Cyber Security, etc. are phenomenal concepts and they take over the market tremendously. Python is one very unique language with a wide range of features. It is implemented in almost every IT enterprise. Today in this article let’s understand in detail a new function in Python called Hash.

What is Python Hash?

Python is a high-level programming language that comes with a variety of built-in library which comprises many functions and modules. Hash in Python is one such module. Hash is used to returning the hash value of any object. In a programming language, the hash is used to return integer values, which are actually utilized in comparing dictionary keys with the help of the dictionary lookup. It calls for _hash_() of an object. While using the hash method the following syntax must be used: hash (object)

Hashing

It is a process where the algorithm is used for mapping data to a fixed-length where data can be of any size.  This is known as a hash value. It is used to produce high performance and direct access to data structures. This enables the storage of a large amount of data that can be accessed quickly and easily.

Python hash() String

In order to better understand the hash method let’s construct a simple scene and example where hash() is used. In the below example we are trying to get the hash value of a string:

name = "Soumya"

hash 1= hash(name)

hash 2= hash(name)

print("Hash 1:%s"% hash 1)

print("Hash 2:%s"% hash 2)

Once you run the above script, you will arrive at the following result:

Soumya: Python scripts soumya$  python  simple_hash.py

Hash1: -6198559587767564025

Hash2: -6198559587767564025

Soumya: Python Scripts soumya$

However, if you run the same thing once again the result changes as follows:

Soumya: Python scripts soumya$  python  simple_hash.py

Hash1: -6198559587767564025

Hash2: -6198559587767564025

Soumya: Python scripts soumya$  python  simple_hash.py

Hash1: -1627844844727594412

Hash2: -1627844844727594412

Soumya: Python Scripts soumya$

Python Hash when the data is slightly modified

If you slightly modify the data used in the above example, let’s see what result the Python will show us:

name 1= "Soumya"

name 2= "Soumya!"

hash 1= hash(name1)

hash 2= hash(name2)

print("Hash 1:%s"% hash 1)

print("Hash 2:%s"% hash 2)

Let’s see the results by running this script:

Soumya: Python scripts soumya$  python  simple_hash.py

Hash1: -4341208365040654583

Hash2: -1405247277152075764

Soumya: Python Scripts soumya$

It is visible that with just change in one character, the hash has changed completely.

Defining hash() for custom objects

Hash() actually works just by overriding the _hash_() function.  However, it is significant to know that not all objects are hashable. Mutable objects cannot be hashed. While hashing for custom objects you must always make sure that hash implementation must not be done for mutable collections. The keys of collections must be immutable for hashing purposes. We need not define a custom _eq_() since it is defined for objects.

Let’s see an example:

class Student:

def_init _ ( self, age, name)

self.age = age

self.name = name

def_eq _ ( self, other)

return self.age ==(other.age and self.name==other.name)

def_hash _(self)

return hash((self.age ,self.name)

student=student(23,'soumya')

print( The hash is: %d % hash(student))

When you run the above script the following result will appear:

Soumya: Python scripts soumya$  python  simple_hash.py

The hash is: -4764809713498807082

Soumya: Python Scripts soumya$

The above example describes how to override both _eq_() and _hash_() and compare your own custom objects.

You can refer the below table to understand how to implement custom hash values:

_eq_()_hash_()Description
If mutable objects are definedDon’t defineHash key value must be immutable
If defined by defaultDefined by defaultAll objects will be compared as unequal
Not definedDon’t defineIn case _eq_() is not defined, then there is no need to define hash as well
DefinedNot defined_hash_() will automatically get adjusted to none and TypeError will be raised
DefinedDo not wish to hash_hash_= None
TypeError will be raised
DefinedRetain from Parent_hash_ = ._hash_

Why mutable objects cannot be Hashed?

The restriction on mutable objects actually simplifies the hash table in huge terms. If in case, mutable objects were permitted for hashing, then the hash table must be updated every single time when the value of an object is updated.  In Python, there are only two objects which use hash tables that are dictionaries and sets.

  • A dictionary is known as an associative array. Only the keys can be hashed in a dictionary and the values cannot be hashed. Hence, a dictionary key must always be an immutable object and the while values can remain anything.
  • A set consists of unique objects that can be hashed. If there are non-hashable objects in a set, you cannot use a set and instead you must consider using a list.

One of the most significant modules in a Python library is the Hash module. You can enroll yourself for our Python training and certification program to get wonderful insights and detailed learning on Python programming language. You will get in-depth knowledge and all-time support for your queries.

Related Blogs:

  1. Brief Overview of Python Language
  2. Python Career opportunities
  3. Python Break Continue
  4. Python Control Flow
  5. Python Data Types
  6. Python Dictionary
  7. Python Exception Handling
  8.  Python File
  9. Python Functions
  10. Python Substring
Besant Technologies WhatsApp