[docs] [issue32118] Doc for comparison of sequences with non-orderable elements

Terry J. Reedy report at bugs.python.org
Sat Nov 25 17:49:18 EST 2017


Terry J. Reedy <tjreedy at udel.edu> added the comment:

(Raymond, I wrote this before reading your message, but I believe it provides what you requested.  There is nothing special about None that is relevant to sequence comparison.  Nans just happen to be the only built-in non-reflexive objects (that I know of, thank goodness).)

This issue is about order comparisons *also* being defined on a single object in a sequence and therefore also possibly giving a different result.  Title revised to be clearer and short enough to all be visible.  Demonstration:

>>> a = object()
>>> a == a, a != a  # but a < a, a <= a, a >= a, a > a raise TypeError
(True, False)
>>> la = [a]
>>> la == la, la != la, la < la, la <= la, la >= la, la > la
(True, False, False, True, True, False)

Comparison of two sequences infers from identity all 6 rich comparison results on a single object in corresponding positions of the two sequences.

This has nothing to do with the object being a singleton, other than a singleton being a single object.  The enforcement of reflexivity and enforcement of consistent order on single objects are related, but the latter did not have to follow from the first.  The presentation of order, in the patch, by reference to singletons and reflexivity, does not work.  As I said in review, the example is too long and wrongly placed.  I think using a generic object instead of None would also be better.

I would like to rewrite the sequence comparison paragraphs after the first as something like the following.
---

Sequences compare lexicographically using comparison of corresponding elements.  The two objects are first compared for identity.  If they are distinct, they are compared normally.  If they are the same object, the self comparisons are inferred: '==', '>=', and '<=' are true, while '!=', '>', and '<' are false.

By design, the inferred self comparisons for sequences sometimes give different results than would strict element comparison.  Instances of an unordered class become ordered with respect to themselves instead of raising a TypeError.

>>> a = object()
>>> a < a          # By default, objects are not ordered
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: '>=' not supported between instances of 'object' and 'object'
>>> [a] < [a]
False              # Sequence comparison orders an object versus itself

Even anti-reflexive not-a-number values, for example, are treated as reflexive (meaning a == a always).

<insert current nan example> 
---

This re-write describes sequence comparison in one paragraph, instead of putting the details in the middle of the next paragraph.  The next paragraph describes the two possible consequences of inferring comparisons from identify: a result instead of a raise, and a different result.

----------
nosy: +terry.reedy
title: Docs: add note about sequence comparisons containing non-orderable elements -> Doc for comparison of sequences with non-orderable elements

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue32118>
_______________________________________


More information about the docs mailing list