set集合去重机制:先调用hash,若发现hash出的内存地址已被占用,会再次调用eq比较内容是否相同.
__hash__对与同一个值的同一次运算的结果是相同的
class Employee:
def __init__(self,name,age,sex,partment):
self.name = name
self.age = age
self.sex = sex
self.partment = partment
def __hash__(self):
return hash('%s%s'%(self.name,self.sex))
def __eq__(self, other):
if self.name == other.name and self.sex == other.sex:
return True
employ_lst = []
for i in range(200):
employ_lst.append(Employee('alex',i,'male','python'))
for i in range(200):
employ_lst.append(Employee('wusir',i,'male','python'))
for i in range(200):
employ_lst.append(Employee('taibai', i, 'male', 'python'))
# print(employ_lst)
employ_set = set(employ_lst)
for person in employ_set:
print(person.__dict__)