Closed sdachen closed 2 weeks ago
Upon further testing, I found
>>> A.__eq__(a11, a12)
NotImplemented
>>> A.__eq__(a11, a11)
True
>>> a11.__eq__(a12)
NotImplemented
>>> a11.__eq__(a11)
True
Looks like it's not really a bug in the documentation, but why would
a11 == a12
not call a11.__eq__(a12)
here?
Reopening for another paragraph that mentions x==y
calls x.__eq__(y)
.
These are the so-called “rich comparison” methods. The correspondence between operator symbols and method names is as follows: x<y
calls x.__lt__(y)
, x<=y
calls x.__le__(y)
, x==y
calls x.__eq__(y)
, x!=y
calls x.__ne__(y)
, x>y
calls x.__gt__(y)
, and x>=y
calls x.__ge__(y)
.
I think maybe it's because the last paragraph of the fall back behavior:
When no appropriate method returns any value other than NotImplemented, the ==
and !=
operators will fall back to is and is not, respectively.
Original paragraph from this page:
By default, object implements eq() by using is, returning NotImplemented in the case of a false comparison:
True if x is y else NotImplemented
.--
If I understand correctly, this means if no
__eq__()
is implemented for a custom class, it'll defaultobject.__eq__()
to the implementationTrue if x is y else NotImplemented
.However, upon testing, I found the behavior to be different.
The program yields
Showing an inconsistency between the code from documentation and the actual implementation in
Python 3.12.3 (main, Apr 9 2024, 08:09:14) [Clang 15.0.0 (clang-1500.3.9.4)]
.