make an object hashable python code example
Example: python make an object hashable
class Hero:
def __init__(self, name, age):
self.name = name
self.age = age
def __str__(self):
return self.name + str(self.age)
def __hash__(self):
print(hash(str(self)))
return hash(str(self))
def __eq__(self,other):
return self.name == other.name and self.age== other.age
heroes = set()
heroes.add(Hero('Zina Portnova', 16))
print(len(heroes))
heroes.add(Hero('Lara Miheenko', 17))
print(len(heroes))
heroes.add(Hero('Zina Portnova', 16))
print(len(heroes))