495

在编写自定义类时,通过==and!=运算符允许等价通常很重要。在 Python 中,这可以通过分别实现__eq____ne__特殊方法来实现。我发现最简单的方法是以下方法:

class Foo:
    def __init__(self, item):
        self.item = item

    def __eq__(self, other):
        if isinstance(other, self.__class__):
            return self.__dict__ == other.__dict__
        else:
            return False

    def __ne__(self, other):
        return not self.__eq__(other)

你知道这样做的更优雅的方法吗?您知道使用上述比较__dict__s 的方法有什么特别的缺点吗?

注意:澄清一下——当__eq____ne__未定义时,你会发现这种行为:

>>> a = Foo(1)
>>> b = Foo(1)
>>> a is b
False
>>> a == b
False

也就是说,因为它确实运行,所以a == b评估为身份测试(即,“与 相同的对象吗?”)。Falsea is bab

__eq____ne__被定义时,你会发现这种行为(这是我们所追求的):

>>> a = Foo(1)
>>> b = Foo(1)
>>> a is b
False
>>> a == b
True
4

11 回答 11

417

考虑这个简单的问题:

class Number:

    def __init__(self, number):
        self.number = number


n1 = Number(1)
n2 = Number(1)

n1 == n2 # False -- oops

因此,Python 默认使用对象标识符进行比较操作:

id(n1) # 140400634555856
id(n2) # 140400634555920

覆盖该__eq__函数似乎可以解决问题:

def __eq__(self, other):
    """Overrides the default implementation"""
    if isinstance(other, Number):
        return self.number == other.number
    return False


n1 == n2 # True
n1 != n2 # True in Python 2 -- oops, False in Python 3

Python 2中,请始终记住也要覆盖该__ne__函数,如文档所述:

比较运算符之间没有隐含的关系。的真理x==y并不意味着它x!=y是错误的。因此,在定义 时__eq__(),还应该定义__ne__()操作符的行为与预期一致。

def __ne__(self, other):
    """Overrides the default implementation (unnecessary in Python 3)"""
    return not self.__eq__(other)


n1 == n2 # True
n1 != n2 # False

Python 3中,这不再是必要的,正如文档所述:

默认情况下,__ne__()委托__eq__()并反转结果,除非它是NotImplemented. 比较运算符之间没有其他隐含关系,例如,真值(x<y or x==y)不隐含x<=y

但这并不能解决我们所有的问题。让我们添加一个子类:

class SubNumber(Number):
    pass


n3 = SubNumber(1)

n1 == n3 # False for classic-style classes -- oops, True for new-style classes
n3 == n1 # True
n1 != n3 # True for classic-style classes -- oops, False for new-style classes
n3 != n1 # False

注意: Python 2 有两种类:

  • 经典风格(或旧风格)类,继承自object声明为class A:class A():或者class A(B):哪里B是经典风格类;

  • 新式类,继承自object并声明为class A(object)class A(B):在哪里B是新式类。class A:Python 3 仅具有声明为、class A(object):或的新型类class A(B):

对于经典风格的类,比较操作总是调用第一个操作数的方法,而对于新风格的类,它总是调用子类操作数的方法,而不管操作数的顺序如何

所以在这里,ifNumber是一个经典风格的类:

  • n1 == n3来电n1.__eq__
  • n3 == n1来电n3.__eq__
  • n1 != n3来电n1.__ne__
  • n3 != n1来电n3.__ne__

如果Number是新式类:

  • 两者n1 == n3n3 == n1调用n3.__eq__
  • 两者n1 != n3n3 != n1打电话n3.__ne__

为了解决Python 2 经典样式类的==and运算符的非交换性问题,and方法应该在不支持操作数类型时返回值。该文档将值定义为:!=__eq____ne__NotImplementedNotImplemented

如果数值方法和富比较方法没有实现对提供的操作数的操作,则它们可能会返回此值。(然后解释器将尝试反射操作或其他一些回退操作,具体取决于操作员。)其真值为真。

在这种情况下,运算符将比较操作委托给另一个操作数的反射方法。该文档将反射方法定义为:

这些方法没有交换参数版本(当左参数不支持操作但右参数支持时使用);更确切地说,__lt__()__gt__()是彼此的反映,__le__()并且__ge__()是彼此的反映, __eq__()并且__ne__()是自己的反映。

结果如下所示:

def __eq__(self, other):
    """Overrides the default implementation"""
    if isinstance(other, Number):
        return self.number == other.number
    return NotImplemented

def __ne__(self, other):
    """Overrides the default implementation (unnecessary in Python 3)"""
    x = self.__eq__(other)
    if x is NotImplemented:
        return NotImplemented
    return not x

如果操作数是不相关类型(无继承)时需要and运算符的可交换性,即使对于新型类,返回NotImplemented值而不是也是正确的做法。False==!=

我们到了吗?不完全的。我们有多少个独特的数字?

len(set([n1, n2, n3])) # 3 -- oops

集合使用对象的哈希值,默认情况下 Python 返回对象标识符的哈希值。让我们尝试覆盖它:

def __hash__(self):
    """Overrides the default implementation"""
    return hash(tuple(sorted(self.__dict__.items())))

len(set([n1, n2, n3])) # 1

最终结果如下所示(我在最后添加了一些断言进行验证):

class Number:

    def __init__(self, number):
        self.number = number

    def __eq__(self, other):
        """Overrides the default implementation"""
        if isinstance(other, Number):
            return self.number == other.number
        return NotImplemented

    def __ne__(self, other):
        """Overrides the default implementation (unnecessary in Python 3)"""
        x = self.__eq__(other)
        if x is not NotImplemented:
            return not x
        return NotImplemented

    def __hash__(self):
        """Overrides the default implementation"""
        return hash(tuple(sorted(self.__dict__.items())))


class SubNumber(Number):
    pass


n1 = Number(1)
n2 = Number(1)
n3 = SubNumber(1)
n4 = SubNumber(4)

assert n1 == n2
assert n2 == n1
assert not n1 != n2
assert not n2 != n1

assert n1 == n3
assert n3 == n1
assert not n1 != n3
assert not n3 != n1

assert not n1 == n4
assert not n4 == n1
assert n1 != n4
assert n4 != n1

assert len(set([n1, n2, n3, ])) == 1
assert len(set([n1, n2, n3, n4])) == 2
于 2014-08-07T07:24:42.003 回答
220

您需要小心继承:

>>> class Foo:
    def __eq__(self, other):
        if isinstance(other, self.__class__):
            return self.__dict__ == other.__dict__
        else:
            return False

>>> class Bar(Foo):pass

>>> b = Bar()
>>> f = Foo()
>>> f == b
True
>>> b == f
False

更严格地检查类型,如下所示:

def __eq__(self, other):
    if type(other) is type(self):
        return self.__dict__ == other.__dict__
    return False

除此之外,您的方法可以正常工作,这就是特殊方法的用途。

于 2008-12-24T02:30:13.353 回答
162

你描述的方式是我一直这样做的方式。由于它是完全通用的,因此您始终可以将该功能分解为一个 mixin 类,并在您想要该功能的类中继承它。

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
于 2008-12-24T00:44:13.270 回答
17

不是一个直接的答案,但似乎足够相关,可以附加,因为它有时会节省一些冗长的乏味。直接从文档中删除...


functools.total_ordering(cls)

给定一个定义一个或多个丰富的比较排序方法的类,这个类装饰器提供其余的。这简化了指定所有可能的丰富比较操作所涉及的工作:

该类必须定义__lt__()__le__()__gt__()或之一__ge__()。此外,该类应该提供一个__eq__()方法。

2.7 版中的新功能

@total_ordering
class Student:
    def __eq__(self, other):
        return ((self.lastname.lower(), self.firstname.lower()) ==
                (other.lastname.lower(), other.firstname.lower()))
    def __lt__(self, other):
        return ((self.lastname.lower(), self.firstname.lower()) <
                (other.lastname.lower(), other.firstname.lower()))
于 2012-09-19T12:19:09.897 回答
9

您不必同时覆盖两者__eq____ne__您只能覆盖__cmp__,但这将对 ==、!==、<、> 等的结果产生影响。

is测试对象身份。这意味着 a isb 将出现True在 a 和 b 都持有对同一对象的引用的情况下。在 python 中,您总是在变量中保存对对象的引用,而不是实际对象,因此基本上对于 a is b 来说,它们中的对象应该位于相同的内存位置。你如何以及最重要的是为什么要覆盖这种行为?

编辑:我不知道__cmp__从 python 3 中删除了所以避免它。

于 2008-12-23T22:44:26.430 回答
6

从这个答案:https ://stackoverflow.com/a/30676267/541136我已经证明了这一点,虽然__ne__用术语定义是正确的__eq__- 而不是

def __ne__(self, other):
    return not self.__eq__(other)

你应该使用:

def __ne__(self, other):
    return not self == other
于 2015-06-17T00:30:12.723 回答
4

我认为您正在寻找的两个术语是平等(==) 和身份(is)。例如:

>>> a = [1,2,3]
>>> b = [1,2,3]
>>> a == b
True       <-- a and b have values which are equal
>>> a is b
False      <-- a and b are not the same list object
于 2008-12-23T23:12:07.920 回答
2

'is' 测试将使用内置的 'id()' 函数测试身份,该函数本质上返回对象的内存地址,因此不可重载。

但是,在测试类的相等性的情况下,您可能希望对测试更加严格,并且只比较类中的数据属性:

import types

class ComparesNicely(object):

    def __eq__(self, other):
        for key, value in self.__dict__.iteritems():
            if (isinstance(value, types.FunctionType) or 
                    key.startswith("__")):
                continue

            if key not in other.__dict__:
                return False

            if other.__dict__[key] != value:
                return False

         return True

此代码将仅比较您的类的非函数数据成员,并跳过通常是您想要的任何私有数据。在普通旧 Python 对象的情况下,我有一个实现 __init__、__str__、__repr__ 和 __eq__ 的基类,因此我的 POPO 对象不会承担所有额外(并且在大多数情况下相同)逻辑的负担。

于 2008-12-24T02:55:28.950 回答
2

我喜欢使用通用类装饰器,而不是使用子类化/混合

def comparable(cls):
    """ Class decorator providing generic comparison functionality """

    def __eq__(self, other):
        return isinstance(other, self.__class__) and self.__dict__ == other.__dict__

    def __ne__(self, other):
        return not self.__eq__(other)

    cls.__eq__ = __eq__
    cls.__ne__ = __ne__
    return cls

用法:

@comparable
class Number(object):
    def __init__(self, x):
        self.x = x

a = Number(1)
b = Number(1)
assert a == b
于 2019-03-15T11:47:55.363 回答
2

这包含了对 Algorias 答案的评论,并通过单个属性比较对象,因为我不关心整个 dict。hasattr(other, "id")一定是真的,但我知道这是因为我在构造函数中设置了它。

def __eq__(self, other):
    if other is self:
        return True

    if type(other) is not type(self):
        # delegate to superclass
        return NotImplemented

    return other.id == self.id
于 2019-10-31T03:05:32.290 回答
0

我写了一个自定义基础,它的默认实现__ne__简单地否定了__eq__

class HasEq(object):
  """
  Mixin that provides a default implementation of ``object.__neq__`` using the subclass's implementation of ``object.__eq__``.

  This overcomes Python's deficiency of ``==`` and ``!=`` not being symmetric when overloading comparison operators
  (i.e. ``not x == y`` *does not* imply that ``x != y``), so whenever you implement
  `object.__eq__ <https://docs.python.org/2/reference/datamodel.html#object.__eq__>`_, it is expected that you
  also implement `object.__ne__ <https://docs.python.org/2/reference/datamodel.html#object.__ne__>`_

  NOTE: in Python 3+ this is no longer necessary (see https://docs.python.org/3/reference/datamodel.html#object.__ne__)
  """

  def __ne__(self, other):
    """
    Default implementation of ``object.__ne__(self, other)``, delegating to ``self.__eq__(self, other)``.

    When overriding ``object.__eq__`` in Python, one should also override ``object.__ne__`` to ensure that
    ``not x == y`` is the same as ``x != y``
    (see `object.__eq__ <https://docs.python.org/2/reference/datamodel.html#object.__eq__>`_ spec)

    :return: ``NotImplemented`` if ``self.__eq__(other)`` returns ``NotImplemented``, otherwise ``not self.__eq__(other)``
    """
    equal = self.__eq__(other)
    # the above result could be either True, False, or NotImplemented
    if equal is NotImplemented:
      return NotImplemented
    return not equal

如果你从这个基类继承,你只需要实现__eq__和基类。

回想起来,更好的方法可能是将其实现为装饰器。就像是@functools.total_ordering

于 2021-07-03T03:17:01.847 回答