[Python-3000] Please re-add __cmp__ to python 3000

Adam Olsen rhamph at gmail.com
Wed Oct 31 04:13:03 CET 2007


On 10/30/07, Steven Bethard <steven.bethard at gmail.com> wrote:
> On 10/30/07, Adam Olsen <rhamph at gmail.com> wrote:
> > On 10/30/07, Steven Bethard <steven.bethard at gmail.com> wrote:
> > > On 10/30/07, Adam Olsen <rhamph at gmail.com> wrote:
> > > > cmp and __cmp__ are doomed, due to unorderable types now raising exceptions:
> > > >
> > > > >>> cmp(3, 'hello')
> > > > Traceback (most recent call last):
> > > >   File "<stdin>", line 1, in <module>
> > > > TypeError: unorderable types: int() < str()
> > > > >>> 3 == 'hello'
> > > > False
> > > >
> > > > A mixin for __cmp__ would be sufficient for scalars (where you can
> > > > avoid this exception and your size is constant), but not for
> > > > containers (which need to avoid inappropriate types and wish to avoid
> > > > multiple passes.)
> > >
> > > I don't understand this conclusion.  If you start comparing things
> > > that are unorderable, you'll get an exception.  But cmp() still makes
> > > sense when you compare other things::
> > >
> > >     >>> cmp((1, 'a', 4.5), (1, 'a', 6.2))
> > >     -1
> > >     >>> cmp([6, 5, 4], [6, 4, 5])
> > >     1
> > >
> > > I definitely don't want any cmp/__cmp__ implementation that swallows
> > > exceptions when the types don't align, e.g.::
> > >
> > >     >>> cmp((1, 'a'), ('a', 1))
> > >     Traceback (most recent call last):
> > >       File "<stdin>", line 1, in <module>
> > >     TypeError: unorderable types: int() < str()
> >
> > What I meant is that you can't use a mixin to map __eq__ to __cmp__,
> > as you'll get TypeError even though == is defined for those types.
>
> I wasn't suggesting that, though I don't see why a mixin would fail
> here assuming you have both __eq__ and __lt__.  Just to the __lt__
> comparison first.
>
> I'm actually currently in favor of keeping __cmp__ as it is in Python
> 2.5.  If a class defines only __cmp__, Python will do the appropriate
> dance to make <, >, ==, etc. work right.  If a class defines only
> __eq__, __lt__, etc. Python will do the appropriate dance to make
> cmp() work right.

For some definition of "right".  A container defines only __cmp__,
using cmp() internally, will be broken in 3.0.


-- 
Adam Olsen, aka Rhamphoryncus


More information about the Python-3000 mailing list