| by Arround The Web | No comments

Python hash() Method

Hashing” is a technique that transforms a “key value” into a “hash value”, which can enhance the security of the key. In hashing, a string is mapped to another value utilizing a specified function. One such function is the “hash()” function in Python that retrieves the hash value of the hashable object when passed as an argument.

This Python will present a thorough guide on the “hash()” function using numerous examples with the help of the following supported content aspects:

What is the “hash()” Method in Python?

In Python, the “hash()” method retrieves the hash value of a particular object such that a dictionary look is carried out quickly using hash values, which are just integers.

Syntax

hash(object)

Parameter

In the above syntax, the “object” parameter indicates an object whose hash value is to be retrieved. There are several types of objects, including integers, strings, lists, tuples, and so on.

Return Value

The “hash()” method retrieves an integer that denotes the hash of the input object.

Example 1: Determining Hash Value Using the “hash()” Method

The below code is utilized to compute the hash value of the input value:

value = "Python Guide"
print(hash(value))

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

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

In the above code, the “hash()” method takes the string, int, and float values as its arguments, respectively, and retrieves the corresponding hash value.

Output

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

Example 2: Determining the Hash Value of Mutable and Immutable Objects

The following code is utilized to determine the hash value of the mutable and immutable objects:

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

value2 = [71, 32, 43, 84, 35]
print('Hash Value of List(Mutable): ', hash(value2))

In the above code, the hash value of the immutable “Tuple” is returned by the “hash()” method while the hash value of the mutable “List” throws an “Unhashable” error.

Output

The “hash()” method retrieves the hash value of the “immutable” object but retrieves an “unhashable” error when the mutable object is passed.

Example 3: Using the “hash()” Method on Custom Object

The following code implements the “hash()” method on the custom object:

class students:
    def __init__(self, name, age):
        self.name = name
        self.age = age
    def __eq__(self, other):
        return self.name == other.name and self.age == other.age
    def __hash__(self):
        return hash((self.name, self.age))

name_1 = students("Joseph", 18)
print(hash(name_1))
name_2 = students("Lily", 28)
print(hash(name_2))

In the above code lines:

  • The class named “students” with two attributes “name” and “age” is defined in the code.
  • The three special methods called “__init__”, “__eq__” and “__hash__” are defined inside the class, respectively.
  • The “__init__” method is the constructor that initializes the attributes based on the arguments passed.
  • The “__eq__” method is the equality operator that compares two instances of the class by their attributes. It returns “True” if both instances have the same “name” and “age”, and “False” otherwise.
  • The “__hash__” method returns a hash value for a class instance. It uses the built-in function “hash()” to compute a hash value based on a tuple of attributes.
  • The two instances named “name_1” and “name_2” are created.
  • The “hash()” function takes the “object” values as an argument and determines the hash value of both instances.

Output

The hash value of the custom object has been determined accordingly in both cases.

Conclusion

In Python, the “hash()” function takes the hashable object such as “str”, “int”, “float”, etc. as an argument and retrieves the integer type hash value. The non-hashable object such as “list”, “set”, “dict”, etc. throws an “unhashable” error when passed as an argument to the “hash()” function. This write-up delivered a comprehensive tutorial on the “hash()” function using relevant examples.

Share Button

Source: linuxhint.com

Leave a Reply