Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

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)) # gets hash -8926039986155829407
print(len(heroes)) # gets 1

heroes.add(Hero('Lara Miheenko', 17)) # gets hash -2822451113328084695
print(len(heroes)) # gets 2

heroes.add(Hero('Zina Portnova', 16)) # gets hash -8926039986155829407
print(len(heroes)) # gets 2 
Comment

PREVIOUS NEXT
Code Example
Python :: how to execute bash commands in python script 
Python :: button in python 
Python :: python one line if statement without else 
Python :: python list transpose 
Python :: get column pandas 
Python :: ValueError: Found array with dim 3. Estimator expected <= 2. 
Python :: connect to spark cluster 
Python :: Python How To Check Operating System 
Python :: args kwargs python 
Python :: text widget get tkinter 
Python :: python count items in list 
Python :: trim starting space python 
Python :: redirect parameters flask 
Python :: python append filename to path 
Python :: How to convert string date to datetime format in python 
Python :: How are iloc and loc different? 
Python :: To visualize the correlation between any two columns | scatter plot graph 
Python :: current date and time django template 
Python :: how to import opencv in python 
Python :: python print 2 decimal places 
Python :: run matlab code in python 
Python :: print flush python 
Python :: python pil 
Python :: Find and count unique values of a single column in Pandas DataFrame 
Python :: raspberry pi keyboard python input 
Python :: python replace line in file 
Python :: python filter data from list 
Python :: python check if there is internet connection 
Python :: pyplot python 
Python :: count_values in python 
ADD CONTENT
Topic
Content
Source link
Name
8+8 =