[issue23699] Add a macro to ease writing rich comparisons

Petr Viktorin report at bugs.python.org
Mon May 18 16:55:56 CEST 2015


Petr Viktorin added the comment:

Conceptually there's a distinction between the two cases, but you can implement one in terms of the other, so I don't think it's worth adding two functions/macros here. So let's pick the better API.

"Py_cmp_to_bool" is better if you already have a cmp-style result. Python code is full of cmp-style results, but I think a big reason is that py2 required them, and (rightly) nobody wants to rewrite the algorithms. I believe the py3 way of passing in the operator is better API.

I've seen (a - b) far too many times, which gives the right result in most but *not all* cases. (Think small floats where the difference is coerced to int for the Py_cmp_to_bool function. Or int overflow.)
The correct ways to get a cmp-style result are "(a > b) - (a < b)" or "(a < b) ? -1 : (a > b)". Do we want to add a function that requires you to write, read, and understand that?

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue23699>
_______________________________________


More information about the Python-bugs-list mailing list