Python 2.x breaks cmp() (was Re: A suspected bug)

Grant Edwards grante at visi.com
Tue Feb 20 21:19:15 CET 2001


In article <slrn993luj.24c.ssthapa at ntcs-ip45.uchicago.edu>, ssthapa at classes.cs.uchicago.edu wrote:
>Aahz Maruch <aahz at panix.com> wrote:
>>>>>> L=[1j, 2j]
>>>>>> L.sort()
>>>Traceback (most recent call last):
>>>  File "<stdin>", line 1, in ?
>>>TypeError: cannot compare complex numbers using <, <=, >, >=
>>>>>>
>>
>>Is there any chance this can be treated as a bug and fixed for
>>the release of 2.1?  Alternatively, given that we're already
>>breaking code with the change in the way complex numbers are
>>handled, should cmp() now raise an exception *every* time the
>>type/class differs?
>
>If you are referring to the comparision of complex numbers, I'm
>not sure how the ordering would be done.

Understood.  But, for all of the other pairs of objects where
nobody is sure how the ordering should done an arbitrary,
consistent order is chosen rather than an exception being
raised.

>For example consider 1 and 1j, they have the same magnitude so
>mathematically speaking there is no way to consistently order
>them like the real numbers where magnitude is a more useful
>measure. IIRC, for any given magnitude n there are an infinite
>number of complex numbers with that magnitude, namely all
>n*e^(ix) for real x.

One could argue that there is no consistent way to order 1.23
and "Winnie the Pooh", but it is done anyway.  Complex numbers
should behave like everything else.

cmp() should always return an ordering, or always raise an
exception if the comparison is not meaningful.  I don't
particularly care which, but it ought to be consistent in order
to avoid violating the "law of least astonishment."

[I've recently read that Unicode strings are also broken in
this respect -- IMO, that needs to be fixed also.]

-- 
Grant Edwards                   grante             Yow!  Yow! Am I cleansed
                                  at               yet?!
                               visi.com            



More information about the Python-list mailing list