Obsolesence of <> (fwd)

Tim Peters tim.one at home.com
Fri Jun 1 17:15:45 EDT 2001


]Lulu of the Lotus-Eaters]
> Here's an obvious way to well-order two complex numbers:
>
>     def lexigraphic_order(c1,c2):
>         if c1.real < c2.real: return -1
>         elif c1.real > c2.real: return 1
>         else: return cmp(c1.imag,c2.imag)
> ...

Easier is to exploit Python's lexicographic comparison of tuples directly:

    return cmp((c1.real, c1.imag), (c2.real, c2.imag))

Python *used* to do that for complexes.  At the very start of the language,
it was impossible (for internal technical reasons) to raise any exception
from a comparison function, so at the start all objects had to support
consistent (not to say meaningful) comparisons.  It later became possible to
raise exceptions from comparisons, but still not possible for a comparison
function to determine which specific comparison was desired.  When "rich
comparisons" got added even later, it finally became possible to distinguish
== and != from the others, and at that point Guido was eager to stop
returning nonsense results for ordered comparisons of complex numbers.  Not
a pure win, and debatable whether it was even a net win, but so it goes.





More information about the Python-list mailing list