python 3's adoption

Terry Reedy tjreedy at udel.edu
Fri Jan 29 00:56:26 EST 2010


On 1/28/2010 6:47 PM, Paul Boddie wrote:
> On 27 Jan, 13:26, Xah Lee<xah... at gmail.com>  wrote:
>>
>> So, for practical reasons, i think a “key” parameter is fine. But
>> chopping off “cmp” is damaging. When your data structure is complex,
>> its order is not embedded in some “key”. Taking out “cmp” makes it
>> impossible to sort your data structure.
>
> What would annoy me if I used Python 3.x would be the apparent lack of
> the __cmp__ method for conveniently defining comparisons between
> instances of my own classes. Having to define all the rich comparison
> methods frequently isn't even as much fun as it sounds.

You define __eq__, which automatically generates __ne__.
You define __lt__, which is all sort and heap need.
This should be about as easier than __eq__, which is really needed, and 
__cmp__. If you need the other 3, either copy the recipe in the 
Cookbook, or, even faster

def __ge__(s,o): return o.__lt__(s)
def __le__(s,o): return s < o or s == o
def __ge__(s,o): return s > o or s == o

All done.

Terry Jan Reedy







More information about the Python-list mailing list