另一个问题为实现对象相等性测试提供了一个很好、简单的解决方案。我将重复上下文的答案:
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
我想为使用__slots__
. 我知道基类和子类都必须使用插槽,但是您将如何定义__eq__
它以使用插槽?