HomeLinuxPython hash() Methodology

Python hash() Methodology


Hashing” is a method that transforms a “key worth” right into a “hash worth”, which may improve the safety of the important thing. In hashing, a string is mapped to a different worth using a specified perform. One such perform is the “hash()” perform in Python that retrieves the hash worth of the hashable object when handed as an argument.

This Python will current an intensive information on the “hash()” perform utilizing quite a few examples with the assistance of the next supported content material facets:

What’s the “hash()” Methodology in Python?

In Python, the “hash()” methodology retrieves the hash worth of a selected object such {that a} dictionary look is carried out shortly utilizing hash values, that are simply integers.

Syntax

Parameter

Within the above syntax, the “object” parameter signifies an object whose hash worth is to be retrieved. There are a number of sorts of objects, together with integers, strings, lists, tuples, and so forth.

Return Worth

The “hash()” methodology retrieves an integer that denotes the hash of the enter object.

Instance 1: Figuring out Hash Worth Utilizing the “hash()” Methodology

The under code is utilized to compute the hash worth of the enter worth:

worth = “Python Information”
print(hash(worth))

value1 = 45
print(n,hash(value1))

value3 = 33.15
print(n,hash(value3))

Within the above code, the “hash()” methodology takes the string, int, and float values as its arguments, respectively, and retrieves the corresponding hash worth.

Output

The corresponding hash values of the initialized values have been displayed within the above output.

Instance 2: Figuring out the Hash Worth of Mutable and Immutable Objects

The next code is utilized to find out the hash worth of the mutable and immutable objects:

value1 = (41, 52, 33, 74, 25)
print(‘Hash Worth of Tuple(Immutable): ‘, hash(value1))

value2 = [71, 32, 43, 84, 35]
print(‘Hash Worth of Record(Mutable): ‘, hash(value2))

Within the above code, the hash worth of the immutable “Tuple” is returned by the “hash()” methodology whereas the hash worth of the mutable “Record” throws an “Unhashable” error.

Output

The “hash()” methodology retrieves the hash worth of the “immutable” object however retrieves an “unhashable” error when the mutable object is handed.

Instance 3: Utilizing the “hash()” Methodology on Customized Object

The next code implements the “hash()” methodology on the customized object:

class college students:
    def __init__(self, title, age):
        self.title = title
        self.age = age
    def __eq__(self, different):
        return self.title == different.title and self.age == different.age
    def __hash__(self):
        return hash((self.title, self.age))

name_1 = college students(“Joseph”, 18)
print(hash(name_1))
name_2 = college students(“Lily”, 28)
print(hash(name_2))

Within the above code traces:

  • The category named “college students” with two attributes “title” and “age” is outlined within the code.
  • The three particular strategies referred to as “__init__”, “__eq__” and “__hash__” are outlined inside the category, respectively.
  • The “__init__” methodology is the constructor that initializes the attributes primarily based on the arguments handed.
  • The “__eq__” methodology is the equality operator that compares two cases of the category by their attributes. It returns “True” if each cases have the identical “title” and “age”, and “False” in any other case.
  • The “__hash__” methodology returns a hash worth for a category occasion. It makes use of the built-in perform “hash()” to compute a hash worth primarily based on a tuple of attributes.
  • The 2 cases named “name_1” and “name_2” are created.
  • The “hash()” perform takes the “object” values as an argument and determines the hash worth of each cases.

Output

The hash worth of the customized object has been decided accordingly in each circumstances.

Conclusion

In Python, the “hash()” perform takes the hashable object akin to “str”, “int”, “float”, and so on. as an argument and retrieves the integer kind hash worth. The non-hashable object akin to “checklist”, “set”, “dict”, and so on. throws an “unhashable” error when handed as an argument to the “hash()” perform. This write-up delivered a complete tutorial on the “hash()” perform utilizing related examples.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments