[Python-Dev] Deprecated __cmp__ and total ordering

Raymond Hettinger python at rcn.com
Tue Mar 10 15:05:27 CET 2009


[Mart Sõmermaa]
>>> To provide total ordering without __cmp__ one has to implement all of
>>>  __lt__, __gt__, __le__, __ge__, __eq__ and __ne__. However, in all
>>> but a few cases it suffices only to provide a "real" implementation for
>>> e.g. __lt__ and define all the other methods in terms of it as follows:

[Raymond Hettinger]
>> FWIW, I'm working on a solution for the problem using class decorators.
>> The idea is that it would scan a class and fill-in missing methods based
>> on the ones already there.  That way, any one of the four ordering
>> relations can be provided as a starting point and we won't have to
>> annoint one like __lt__ as the one true underlying method.
>>
>> When it's ready, I'll bring it to python-dev for discussion.

[Michael Foord]
> Is there something you don't like about this one: http://code.activestate.com/recipes/576529/

Yes, that recipe has the basic idea!

I think the implementation can be cleaned-up quite a bit
and it can be made as fast as hand-written code (not
the setup time, but the actual introduced method).


Raymond 



More information about the Python-Dev mailing list