hash()
A hash is an fixed sized integer that identifies a particular value.
hash(object) : Returns hash value of the input object ( if available )
hash() value is available for immutable objects ( what is immutable and mutable object ?), but all Hashable are not immutable.
print(hash(300)) # 300
print(hash(300.56)) #1291272085159674156
print(hash('plus2net')) #-4778224534751702784
hash value of tuple
my_tuple=('Alex','Ronald','John')
print(hash(my_tuple)) # 7654376616106661247
hash value of list
my_list=['Alex','Ronald','John']
print(hash(my_list)) # Error
Above code will generate error
TypeError: unhashable type: 'list'
hash() value of user defined object
class student_kit():
college_name='ABC College' #class attributes
#instance attributes
def __init__(self,name,section):
self.name= name
self.section=section
self.height=5 #instance attribute
Alex=student_kit('Alex','A') # object declaration
print(hash(Alex)) # -9223371866271725652
« All Built in Functions in Python
« id() « Mutable & Immutable object
Bitwise operators using hash() »
int()
float()
← Subscribe to our YouTube Channel here
This article is written by plus2net.com team.
https://www.plus2net.com
plus2net.com