Incomparable abominations

Tim Peters tim_one at
Tue Mar 25 04:58:05 CET 2003

[David Mertz, Ph.D.]
> ...
> But the current list sorting behavior is BY FAR the most surprising
> thing in Python!  I would have a big challenge explaining it to a new
> user... heck, I can hardly remember the border cases myself.
> The examples I gave in my first post for this thread show how surprising
> the exceptions are.  I really think that more than half of highly
> experienced Python programmers would have gotten at least one of them
> wrong in their intuition (not you, not Alex... maybe not Guido :-)...
> but past that, mistakes).

I expect I would have gotten most of them wrong, and I confess I wasn't
interested enough to guess at what would happen.  Why would I?  I asked last
time what your use case is:  for what purpose do you think you need to be
able to sort lists with a mix of element types for which Python can't dream
up a better ordering than comparing the objects' memory addresses?  If you
don't have a real use case, I don't care about this.

> I guess part of what goes into this is different intuitions about the
> meaning of sorting.  I put sorting a list into a different conceptual
> category than comparing individual items (while knowing, of course, the
> underlying relatedness of the concepts).  Sorting, to me, just means a
> stable (but arbitrary) ordering.  Comparison with "<" and friends
> implie, to me, a "natural" ordering.  I wouldn't mind if "<" stopped
> comparing anything without an obviously correct answer.

What's your use case?

> Even then, no matter how you cut it, the relation "1j < 2j" is
> self-evident and natural.  How can I explain to anyone why that is an
> error rather than a True result?!

It's like faith:  if they know anything about complex numbers, no
explanation is necessary; if they don't know anything about complex numbers,
not only is no explanation possible, but they don't have a use case for it
either so it's not a real problem for them <0.5 wink>.

Since the members of a complex numbers are floats, then if 1j < 2j is
self-evident, what's the self-evident result for 1e-300+1j < 2j?  Is one
self-evident and the other arbitrary?  Etc.  There are *useful* orderings on
complex numbers, but no "most useful" ordering, and no ordering at all is
conventionally defined on them.

More information about the Python-list mailing list