25.07.19 23:19, Kyle Stanley пише:
Serhiy Storchaka wrote:
Actually, the == operator cannot return NotImplemented.
Thanks for the clarification. What is the reason for this limitation and is it only possible for the `==` operator to return one of `None`, `False`, or `True`?
The `==` operator can return any value except NotImplemented. But all implementations in the stdlib return only booleans. NumPy is the one famous exception.
It seems like it would be useful for it to be able to return `NotImplemented` in situations such as this.
It is the purpose of NotImplemented. It signals "ignore me, use other way to evaluate a result".
Also, I think that I may have had some misconceptions with regards to the relationship between the `__eq__()` method and the `==` operator. I know they are not the same, but isn't the result of the `==` operator based on a transformation of the result from `__eq__()`?
Yes, it is. And NotImplemented means that the result of `__eq__()` (or other dunder methods) should be ignored.
As far as I can tell, the equality of two dictionary views are assessing used [`PyObject dictrich_compare`](https://github.com/python/cpython/blob/544fa15ea1b7b73068319bdb217b684e2fd7b...). Wouldn't it be possible to perform a conditional check if the view on the left side of the comparison is a values view and if so, use `Py_RETURN_NOTIMPLEMENTED`?
How does it differ from the default implementation (`object.__eq__`)?