[Python-Dev] Rich comparison of lists and tuples
Guido van Rossum
guido@digicool.com
Wed, 16 May 2001 15:35:46 -0500
[Subject fixed]
[Tim shows there's a lot left to the imagination when trying to glean
the meaning of list1==list2 using rich comparisons.]
I would like to break this down by defining the mapping between cmp()
and rich comparisons.
I propose:
- If cmp() is requested but not defined, and rich comparisons are
defined, try ==, <, > in order; if all three yield false, act as if
rich comparisons were not defined, and use the fallback comparison
(i.e. by address).
- If a rich comparison is requested but not defined, use cmp() and use
the obvious mapping.
- Continue to define the comparison of unequal sequences in terms of
cmp().
- Testing == or != for sequences takes these shortcuts:
1. if the lengths differ, the sequences differ
2. compare the elements using == until a false return is found
Note that this defines 'x!=y' as 'not x==y' for sequences. We could
easily go the extra mile and define != to use only != on the items;
but is this worth the extra complexity?
--Guido van Rossum (home page: http://www.python.org/~guido/)