When trying to construct an example where a == b
is not the same as b == a
, it seems that I have accidentally constructed an example where a == b
is not the same as a.__eq__(b)
:
class A:
def __eq__(self, other: object) -> bool:
return type(other) is A
class B(A):
pass
if __name__ == '__main__':
a = A()
b = B()
assert not a.__eq__(b) # as expected
assert not (a == b) # Why does this fail?
Can somebody explain to me why the last assertion fails? I expected it to be the same as the second one.
The relevant quote explaining what happens is located in the documentation:
If the operands are of different types, and the right operand’s type is a direct or indirect subclass of the left operand’s type, the reflected method of the right operand has priority, otherwise the left operand’s method has priority. Virtual subclassing is not considered.