What is the result of returning NotImplemented
from the __eq__
special method in python 3 (fine 3.5, if that matters)?
The documentation is fuzzy; only the relevant text that I found only vaguely refers to "some other fallback":
When NotImplemented
returned, the interpreter will then try the reflected operation on another type or some other backup, depending on the statement. If all attempted operations are returned NotImplemented
, the interpreter will raise a corresponding exception. For more information, see Implementing Arithmetic Operations .
Unfortunately, the "Details" link does not mention __eq__
at all.
My reading of this excerpt suggests that the code below should raise a “corresponding exception”, but that is not the case:
class A: def __eq__(self, other): return NotImplemented class B: def __eq__(self, other): return NotImplemented
From an experiment, I think that when NotImplemented
returns from __eq__
, the interpreter behaves as if __eq__
not defined in the first place (in particular, it swaps the arguments first, and if that doesn't solve the problem, it compares using standard __eq__
, which evaluates to "equal" if both objects have the same identifier). If this is the case, where in the documentation can you find confirmation of this behavior?
Edit: see Python issue 28785
source share