[Python-ideas] cmp and sorting non-symmetric types
Adam Olsen
rhamph at gmail.com
Wed Nov 14 19:47:56 CET 2007
On Nov 14, 2007 10:54 AM, Guido van Rossum <guido at python.org> wrote:
> Are you sure you're solving a real problem?
I see it as part of a problem we've already decided to solve, by
making types with no reasonable ordering raise TypeError.
> On Nov 14, 2007 9:49 AM, Adam Olsen <rhamph at gmail.com> wrote:
> > (ugh, this was supposed to go to python-ideas, not python-list. No
> > wonder I got no responses to this email!)
> >
> > (I've had trouble getting response for collaboration on a PEP.
> > Perhaps I'm the only interested party?)
> >
> > Although py3k raises an exception for completely unsortable types, it
> > continues to silently do the wrong thing for non-symmetric types that
> > overload comparison operator with special meanings.
> >
> > >>> a = set([1])
> > >>> b = set([2, 5])
> > >>> c = set([1, 2])
> > >>> sorted([a, c, b])
> > [{1}, {1, 2}, {2, 5}]
> > >>> sorted([a, b, c])
> > [{1}, {2, 5}, {1, 2}]
> >
> > To solve this I propose a revived cmp (as per the previous thread[1]),
> > which is the preferred path for orderings. The rich comparison
> > operators will be simple wrappers for cmp() (ensuring an exception is
> > raised if they're not merely comparing for equality.)
> >
> > Thus, set would need 7 methods defined (6 rich comparisons plus
> > __cmp__, although it could skip __eq__ and __ne__), whereas nearly all
> > other types (int, list, etc) need only __cmp__.
> >
> > Code which uses <= to compare sets would be assumed to want subset
> > operations. Generic containers should use cmp() exclusively.
> >
> >
> > [1] http://mail.python.org/pipermail/python-3000/2007-October/011072.html
> >
> > --
> > Adam Olsen, aka Rhamphoryncus
> > _______________________________________________
> > Python-ideas mailing list
> > Python-ideas at python.org
> > http://mail.python.org/mailman/listinfo/python-ideas
> >
>
>
>
> --
> --Guido van Rossum (home page: http://www.python.org/~guido/)
>
--
Adam Olsen, aka Rhamphoryncus
More information about the Python-ideas
mailing list