Since Python does not provide left/right versions of its comparison operators, how does it decide which function to call?
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
This seems to call both __eq__ functions.
I am looking for the official decision tree.
The
a == bexpression invokesA.__eq__, since it exists. Its code includesself.value == other. Since int’s don’t know how to compare themselves to B’s, Python tries invokingB.__eq__to see if it knows how to compare itself to an int.If you amend your code to show what values are being compared:
it will print: