how to convert code that uses cmp to python3

Marko Rauhamaa marko at pacujo.net
Fri Apr 8 04:34:00 EDT 2016


Antoon Pardon <antoon.pardon at rece.vub.ac.be>:

> In python2 descending the tree would only involve at most one
> expensive comparison, because using cmp would codify that comparison
> into an integer which would then be cheap to compare with 0. Now in
> python3, I may need to do two expensive comparisons, because there is
> no __cmp__ method, to make such a codefication.

I think you should base your tree implementation on key.__lt__() only.
Only compare keys using <, nothing else, ever.

It may lead to faster or slower performance, depending on the ordering
function, but I think it is more minimalistic, and thus philosophically
more appealing than __cmp__.


Marko



More information about the Python-list mailing list