Cara menggunakan python hash to hex

Python hex() function is used to generate hex value of an integer argument. It takes an integer argument and returns an integer converted into a hexadecimal string. In case, we want to get a hexadecimal value of a float, then use float.hex() function.

Python hex() Function Example

# Calling function  
result = hex(1)   
# integer value  
result2 = hex(342)   
# Displaying result  
print(result)  
print(result2)  

Output:

0x1
0x156

Python hash() Function

Python hash() function is used to get the hash value of an object. Python calculates the hash value by using the hash algorithm. The hash values are integers and used to compare dictionary keys during a dictionary lookup. We can hash only the types which are given below:

The hash() method returns the hash value of an object if it has one. Hash values are just integers that are used to compare dictionary keys during a dictionary look quickly.

Example

text = 'Python Programming'

# compute the hash value of text hash_value = hash(text)

print(hash_value) # Output: -966697084172663693


hash() Syntax

The syntax of hash() method is:

hash(object)

hash() Parameters

The hash() method takes a single parameter:

  • object - the object whose hash value is to be returned (integer, string, float)

hash() Return Value

The hash() method returns the hash value of an object.


Example 1: How hash() works in Python?

# hash for integer unchanged

print('Hash for 181 is:', hash(181))

# hash for decimal

print('Hash for 181.23 is:',hash(181.23))

# hash for string

print('Hash for Python is:', hash('Python'))

Output

Hash for 181 is: 181
Hash for 181.23 is: 530343892119126197
Hash for Python is: 2230730083538390373 

Example 2: hash() for immutable tuple object?

hash() method only works for immutable objects as tuple.

# tuple of vowels
vowels = ('a', 'e', 'i', 'o', 'u')

print('The hash is:', hash(vowels))

Output

The hash is: -695778075465126279

How does hash() work for custom objects?

As stated above, hash() method internally calls

hash(object)
4 method. So, any objects can override
hash(object)
4 for custom hash values.

But for correct hash implementation,

hash(object)
4 should always return an integer. And, both
hash(object)
7 and
hash(object)
4 methods have to be implemented.

Below are the cases for correct

hash(object)
4 override.

__eq__()__hash__()DescriptionDefined (by default)Defined (by default)If left as is, all objects compare unequal (except themselves)(If mutable) DefinedShould not be definedImplementation of hashable collection requires key's hash value be immutableNot definedShould not be definedIf

hash(object)
7 isn't defined,
hash(object)
4 should not be defined.DefinedNot definedClass instances will not be usable as hashable collection. __hash__() implicity set to
# hash for integer unchanged

print('Hash for 181 is:', hash(181))

# hash for decimal

print('Hash for 181.23 is:',hash(181.23))

# hash for string

print('Hash for Python is:', hash('Python'))

2. Raises
# hash for integer unchanged

print('Hash for 181 is:', hash(181))

# hash for decimal

print('Hash for 181.23 is:',hash(181.23))

# hash for string

print('Hash for Python is:', hash('Python'))

3 exception if tried to retrieve the hash.DefinedRetain from Parent
# hash for integer unchanged

print('Hash for 181 is:', hash(181))

# hash for decimal

print('Hash for 181.23 is:',hash(181.23))

# hash for string

print('Hash for Python is:', hash('Python'))

4DefinedDoesn't want to hash
# hash for integer unchanged

print('Hash for 181 is:', hash(181))

# hash for decimal

print('Hash for 181.23 is:',hash(181.23))

# hash for string

print('Hash for Python is:', hash('Python'))

5. Raises TypeError exception if tried to retrieve the hash.

Example 3: hash() for Custom Objects by overriding __hash__()

class Person:
    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): print('The hash is:') return hash((self.age, self.name))

person = Person(23, 'Adam') print(hash(person))

Output

The hash is:
3785419240612877014

Note: You don't have to implement

hash(object)
7 method for the hash as it is created by default for all objects.