What happened to __cmp__() in Python 3.x?

Chris Rebert clp2 at rebertia.com
Sun Aug 16 04:04:59 EDT 2009


On Sat, Aug 15, 2009 at 8:09 AM, Xavier Ho<contact at xavierho.com> wrote:
> Hey all,
>
> I've recently made my way to Python 3.1 and I'm not seeing __cmp__() in the
> documentation.
>
> Is there a substitution for this special method in 3.1, or do I really have
> to define all six rich comparison methods to work it out?

I don't think so. Quoting
http://docs.python.org/dev/py3k/reference/datamodel.html

"There are no swapped-argument versions of [the comparison] methods
(to be used when the left argument does not support the operation but
the right argument does); rather, __lt__() and __gt__() are each
other’s reflection, __le__() and __ge__() are each other’s reflection,
and __eq__() and __ne__() are their own reflection."

I believe this means you only need to define one method from each of
the following pairs to get all the operators working:
* __eq__ or __ne__
* __lt__ or __gt__
* __ge__ or __le__

Cheers,
Chris
-- 
http://blog.rebertia.com



More information about the Python-list mailing list