根据this page, set.intersection 使用该__eq__方法测试元素是否相等。谁能向我解释为什么这会失败?
>>> Class Foo(object):
>>>     def __eq__(self, other):
>>>         return True
>>>
>>> set([Foo()]).intersection([Foo()])
set([])
使用 2.7.3。是否有另一种(不太复杂)的方法来做到这一点?
根据this page, set.intersection 使用该__eq__方法测试元素是否相等。谁能向我解释为什么这会失败?
>>> Class Foo(object):
>>>     def __eq__(self, other):
>>>         return True
>>>
>>> set([Foo()]).intersection([Foo()])
set([])
使用 2.7.3。是否有另一种(不太复杂)的方法来做到这一点?
如果你覆盖,__eq__你也应该总是覆盖__hash__。
“如果 a == b,那么必须是 hash(a) == hash(b),否则集合和字典将失败。” 埃里克
__hash__用于从对象中生成整数。这用于将字典的键或集合的元素放入桶中,以便更快地找到它们。
如果不覆盖__hash__,默认算法会创建不同的哈希整数,尽管对象是相等的。
在你的情况下,我会这样做:
class Foo(object):
    def __eq__(self, other):
        return type(self) == type(other)
    def __hash__(self):
        return 1
因为您的类的所有对象都等于该类的所有其他对象,所以它们必须都在集合中的同一个桶(1)中。这种方式in也返回True。
应该__eq__是什么样子:
如果你只比较 Foo 对象
def __eq__(self, other):
    return self.number == other.number
如果您还将 Foo 对象与其他对象进行比较:
def __eq__(self, other):
    return type(self) == type(other) and self.number == other.number
如果你有不同的类和不同的相等算法,我推荐double-dispatch。
class Foo:
    def __eq__(self, other):
        return hasattr(other, '_equals_foo') and other._equals_foo(self)
    def _equals_foo(self, other):
        return self.number == other.number
    def _equals_bar(self, other):
        return False # Foo never equals Bar
class Bar:
    def __eq__(self, other):
        return hasattr(other, '_equals_bar') and other._equals_bar(self)
    def _equals_foo(self, other):
        return False # Foo never equals Bar
    def _equals_bar(self, other):
        return True # Bar always equals Bar
这样,双方都决定a了平等的含义。ba == b