[Python-Dev] A macro for easier rich comparisons (original) (raw)

David Malcolm dmalcolm at redhat.com
Tue Apr 28 20:12:10 CEST 2015


On Tue, 2015-04-28 at 10:50 -0700, Glenn Linderman wrote:

On 4/28/2015 2:13 AM, Victor Stinner wrote:

_> > #define PyRETURNRICHCOMPARE(val1, val2, op) _ _> > > do { _ _> > > switch (op) { _ _> > > case PyEQ: if ((val1) == (val2)) PyRETURNTRUE; PyRETURNFALSE; _ _> > > case PyNE: if ((val1) != (val2)) PyRETURNTRUE; PyRETURNFALSE; _ _> > > case PyLT: if ((val1) < (val2)) PyRETURNTRUE; PyRETURNFALSE; _ _> > > case PyGT: if ((val1) > (val2)) PyRETURNTRUE; PyRETURNFALSE; _ _> > > case PyLE: if ((val1) <= (val2)) PyRETURNTRUE; PyRETURNFALSE; _ _> > > case PyGE: if ((val1) >= (val2)) PyRETURNTRUE; PyRETURNFALSE; _ _> > > } _ _> > > PyRETURNNOTIMPLEMENTED; _ > > > } while (0) > I would prefer a function for that: > > PyObject *PyRichCompare(long val1, long2, int op); Why would you prefer a function? As a macro, when the op is a constant, most of the code would be optimized away by a decent compiler. I suppose when the op is not a constant, then a function would save code space. So I suppose it depends on the predominant use cases.

There's also the possibility of wrapping C++ code that uses overloaded operators: having it as a macro could allow those C++ operators to be be mapped into Python.

Hope this is constructive Dave



More information about the Python-Dev mailing list