I am using python 3.6. to create two lists. I want to filter it if all of the instances attribute values are the same.
I wrote this code, but it returns false. How to filter it?
class MyCls:
def __init__(self, **kwargs):
self.x = kwargs.get('x')
self.y = kwargs.get('y')
self.z = kwargs.get('z')
def __str__(self):
return str(self.__dict__)
def __hash__(self):
return hash(str(self.__dict__))
def __eq__(self, other):
return str(self.__hash__) == str(other.__hash__)
a = MyCls(x='a', y='b', z='c')
b = MyCls(x='a', y='b', z='c')
ab = [a, b]
print(a is b)
# False
print(a == b)
# False
s = set(ab)
print(s)
# print(2 instances)