[Tutor] True and 1 [was Re: use of the newer dict types]
Steven D'Aprano
steve at pearwood.info
Sun Jul 28 10:33:36 CEST 2013
On 28/07/13 18:07, Alan Gauld wrote:
>> Comparison operators haven't depended on cmp() for a long time. You can
>> google "rich comparison operators" for more info:
>>
>> https://duckduckgo.com/?q=rich%20comparison%20operators
>
> I only read the first two but one question remains:
> If cmp() is gone is __cmp__() still supported? I'm assuming it
> must be for backward compatibility?
Not in Python 3, it's gone. In Python 3, you have to define all six the rich comparison methods __eq__ __ne__ __lt__ __gt__ __le__ __ge__ if you wish to support the comparison operators. There's a helper function in functools to help simplify the job:
@functools.total_ordering
class MyClass:
...
total_ordering will automatically fill in the missing comparison methods using rules like these:
"<=" is equivalent to "not >"
">=" is equivalent to "not <"
"!=" is equivalent to "not =="
"<=" is equivalent to "< or =="
">=" is equivalent to "> or =="
--
Steven
More information about the Tutor
mailing list