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

Toby Dickenson mbel44 at dial.pipex.net
Wed Feb 21 08:00:20 EST 2001


grante at visi.com (Grant Edwards) wrote:

>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.]

In c++ there are two ways to compare something: The operators (> =
etc) all do the obvious thing but are not defined for types where
there is no natural order, such as complex numbers.

Alternatively there is a template 'cmp', which is used for ordering
types even if they dont have a natural order, and is used by the
implementations of set of map templates (C++ maps are tree-based,
similar to pythons hash-based dictionaries).

Im beginning to think that a similar scheme might make sense for
python: It would be a clean way of resolving the current quirky
behaviour of comparing unicode and non-unicode strings.





Toby Dickenson
tdickenson at geminidataloggers.com



More information about the Python-list mailing list