Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #106686

Re: how to convert code that uses cmp to python3

From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Subject Re: how to convert code that uses cmp to python3
Date 2016-04-09 00:25 +1000
Message-ID <mailman.89.1460125529.2253.python-list@python.org> (permalink)
References (5 earlier) <85fuuw5ypl.fsf@benfinney.id.au> <5707B2CE.1010407@rece.vub.ac.be> <CAPTjJmrMv5O5vwrbfHJJCdiHi87naPOanhPmk4Ba_SLRgu1m4Q@mail.gmail.com> <5707BE18.1050805@rece.vub.ac.be> <CAPTjJmoMuqwg0Q_fp=qitC3wWkXYHtdqBNVGzO+3ToAmsgit8Q@mail.gmail.com>

Show all headers | View raw


On Sat, Apr 9, 2016 at 12:20 AM, Antoon Pardon
<antoon.pardon@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

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: how to convert code that uses cmp to python3 Chris Angelico <rosuav@gmail.com> - 2016-04-09 00:25 +1000

csiph-web