[Inada Naoki <[email protected]>]
> FWIW, (list|tuple).__eq__ and (list|tuple).__contains__ uses it too.
> It is very important to compare recursive sequences.
>
> >>> x = []
> >>> x.append(x)
> >>> y = [x]
> >>> z = [x]
> >>> y == z
> True
That's a visible consequence, but I'm afraid this too must be
considered an implementation-defined quirk. If doing a fine job of
comparing recursive containers was the actual goal, we would have
needed to do a much fancier thing, akin to testing graph isomorphism:
>>> a = []
>>> b = []
>>> for x in a, b:
... x.append(x)
>>> a
[[...]]
>>> b
[[...]]
>>> a == a
True
>>> b == b
True
>>> a == b
Traceback (most recent call last):
...
RecursionError: maximum recursion depth exceeded in comparison
Instead the happy behavior in your example (and in my first two
examples) just follows as a no-effort consequence of the
special-casing for identity equality in PyObject_RichCompareBool().
(Note that the _top_ level comparisons in my "a == a" and "b == b"
examples do _not_ exploit that the comparands are the same object -
it's list_richcompare() calling PyObject_RichCompareBool() where the
latter tells the former that a[0] and a[0] are equal.)
_______________________________________________
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/L4RPMHX7USTWLACBC5URKB3WXNBH2DKA/
Code of Conduct: http://python.org/psf/codeofconduct/