Kyle Stanley wrote:
Serhiy Storchaka wrote:
Actually, the == operator cannot return NotImplemented.
What is the reason for this limitation
It's not a limitation, it's a consequence of the way the operator machinery works. NotImplemented is used by operator methods to signal to the interpreter that it should take some alternative action. In this case, it will first try the other operand's __eq__ method, and if that returns NotImplemented as well, it assumes that the operands are not equal and returns False.
and is it only possible for the `==` operator to return one of `None`, `False`, or `True`?
No, it's possible for == to return almost anything (numpy arrays return an array of booleans, for example). It just happens that NotImplemented can't be returned, because it has a special meaning to the interpreter. -- Greg