On Tue, Feb 04, 2020 at 12:33:44PM +1100, Chris Angelico wrote:
[Sebastian Berg]
> > But if `PyObject_RichCompareBool(..., Py_EQ)` is such a fundamental
> > operation (and in a sense it seems to me that it is), is there a point
> > in explicitly defining it?
> >
> > That would mean adding `operator.equivalent(a, b) -> bool` which would
> > allow float to override the result and let
> > `operator.equivalent_value(float("NaN"), float("NaN))` return True;
> > luckily very few types would actually override the operation.
> The implication here is that there would be a corresponding dunder
> method, yes? If it's possible for a type to override it, that would
> need a dunder.
I think the whole point of this is that it *cannot* be overridden.
That's the gist of Raymond's comments about being able to reason about
behaviour. Individual values can override the equality test, but they
cannot override the identity test, and that's a good thing.
Can we summarise this issue like this?
[quote]
Containers or other compound objects are permitted to use identity
testing to shortcut what would otherwise be an equality test (e.g. in
list equality tests, and containment tests), even if that would change
the behaviour of unusual values, such as floating point NANs which
compare unequal to themselves, or objects where `__eq__` have side
effects.
Such containers are permitted to assume that their contents all obey the
reflexivity of equality (each value is equal to itself) and so avoid
calling `__eq__` or `__ne__`.
This is an implementation-specific detail which may differ across
different container types and interpreters.
[end quote]
I don't think we need to make any promises about which specific
containers use this rule. If you need to know, you can test it for
yourself:
if (t:={'a': float('NAN')}) == t:
print('dict equality obeys reflexivity')
but otherwise, most people shouldn't need to care.
[...]
> Can the word "equivalent" be used for this, perhaps?
We don't need and shouldn't have a dunder for this, but the word
"equivalent" would be wrong in any case. Two objects may be equivalent
but not equal, for example, when it comes to iteration, the string "abc"
is equivalent to the list ['a', 'b', 'c'].
I don't think there is any accurate term shorter than "identical or
equal".
--
Steven
_______________________________________________
Python-Dev mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/R3M5UDOLY27QQYIHB4F5H6JPZ2KRZUBL/
Code of Conduct: http://python.org/psf/codeofconduct/