Another question provides a nice, simple solution for implementing a test for equality of objects. I'll repeat the answer for context:
class CommonEqualityMixin(object):
def __eq__(self, other):
return (isinstance(other, self.__class__)
and self.__dict__ == other.__dict__)
def __ne__(self, other):
return not self.__eq__(other)
class Foo(CommonEqualityMixin):
def __init__(self, item):
self.item = item
I would like to do this for a class that uses __slots__
. I understand that both the base class and the subclass will have to use slots, but how would you define __eq__
for this to work with slots?