how to convert code that uses cmp to python3

Mark Lawrence breamoreboy at yahoo.co.uk
Thu Apr 7 10:43:51 EDT 2016


On 07/04/2016 13:05, Antoon Pardon wrote:
> I am looking at my avltree module for converting it to
> python3.
>
> One of the things that trouble me here is how python3 no
> longer has cmp and how things have to be of "compatible"
> type in order to be comparable.
>
> So in python2 it wasn't a problem to have a tree with
> numbers and strings as keys. In python3 that will not
> be so evident.
>
> 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.
>
> Is there a way to work around these limitations or
> should I resign myself to working within them?
>

HTH https://docs.python.org/3/library/functools.html#functools.cmp_to_key

-- 
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence




More information about the Python-list mailing list