how to convert code that uses cmp to python3

Chris Angelico rosuav at gmail.com
Fri Apr 8 10:25:26 EDT 2016


On Sat, Apr 9, 2016 at 12:20 AM, Antoon Pardon
<antoon.pardon at rece.vub.ac.be> wrote:
>> You only need ONE comparison, and the other is presumed to be its
>> opposite. When, in the Python 3 version, would you need to compare
>> twice?
>
> About 50% of the time. When I traverse the tree I go left when the
> argument key is smaller than the node key, I go right when it is
> greater than the node key and I have found the node I want when
> they are equal.

How about this:

You have found the node if they are equal.
Otherwise, go left if your argument is smaller than the node.
Otherwise, go right.

You don't have to do three comparisons, only two - and one of them is
an equality, rather than an inequality, which is often cheaper. But
hey. If you really can't handle the double comparison, *write your
own* special-purpose comparison function - nobody's stopping you! It's
just not something that exists *in the language*. If your specific
objects need this, write it!

ChrisA



More information about the Python-list mailing list