相关疑难解决方法(0)

如何在Python中处理__eq__以及按什么顺序处理?

由于Python不提供其比较运算符的左/右版本,它如何决定调用哪个函数?

class A(object):
    def __eq__(self, other):
        print "A __eq__ called"
        return self.value == other
class B(object):
    def __eq__(self, other):
        print "B __eq__ called"
        return self.value == other

>>> a = A()
>>> a.value = 3
>>> b = B()
>>> b.value = 4
>>> a == b
"A __eq__ called"
"B __eq__ called"
False
Run Code Online (Sandbox Code Playgroud)

这似乎称为两种__eq__功能.只是寻找官方的决策树.

python comparison user-defined

76
推荐指数
3
解决办法
9万
查看次数

标签 统计

comparison ×1

python ×1

user-defined ×1