[issue37685] Fix equality checks for some types
Josh Rosenberg
report at bugs.python.org
Fri Jul 26 14:31:14 EDT 2019
Josh Rosenberg <shadowranger+python at gmail.com> added the comment:
@p-ganssle: Yup. If both sides return NotImplemented, __eq__ and __ne__ return a result based on an identity comparison; all other rich comparisons raise TypeError in that case. Code is here: https://github.com/python/cpython/blob/3.7/Objects/object.c#L679
As you said, NotImplemented is never actually returned from syntax based comparisons; you only see it if you explicitly call __eq__ (and related __XX__ rich comparison methods) directly (usually because you're implementing one method in terms of another).
AFAICT, about the only time you should ever be returning False rather than NotImplemented is when the other operand is an instance of the same class as you (or a subclass), allowing you to say with certainty that you know how to do the comparison (Python already handles the edge case of subclasses for you, by calling the comparison method on the subclass first, even if it's on the right hand side). In any other case, you return NotImplemented, and let the other side determine if they know how to do the comparison.
----------
nosy: +josh.r
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue37685>
_______________________________________
More information about the Python-bugs-list
mailing list