[Tutor] True and 1 [was Re: use of the newer dict types]
Albert-Jan Roskam
fomcl at yahoo.com
Mon Jul 29 13:33:16 CEST 2013
----- Original Message -----
> From: Steven D'Aprano <steve at pearwood.info>
> To: tutor at python.org
> Cc:
> Sent: Sunday, July 28, 2013 10:33 AM
> Subject: Re: [Tutor] True and 1 [was Re: use of the newer dict types]
>
> 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:
Wow, too bad __cmp__ is no longer present. Not so much for cmp(), but because it's such a compact way to define all the comparison methods. Btw, my book says "Python will supply the __ne__() (not equal) inequality operator automatically if wel implement __eq__ but don't implement __ne__()". [Programming in Python 3 (2009), M. Summerfield, p.213]
> @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
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
More information about the Tutor
mailing list