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

由于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

这似乎同时调用了两个__eq__函数。只是在寻找官方决策树。


一只甜甜圈
浏览 868回答 2
2回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python