[Python-checkins] python/dist/src/Objects listobject.c,2.134,2.135
Raymond Hettinger
python@rcn.com
Thu, 5 Sep 2002 11:16:19 -0400
I think they added the three-way if tests to handle cases where
an object instance defined a rich comparison operator and
returned something other than -1, 0, or 1. At one time, I
think 2 and/or -2 had a meaning. But then, I could be confusing
it with another story.
----- Original Message -----
From: "Neal Norwitz" <neal@metaslash.com>
To: <rhettinger@users.sourceforge.net>
Sent: Thursday, September 05, 2002 10:53 AM
Subject: Re: [Python-checkins] python/dist/src/Objects listobject.c,2.134,2.135
> rhettinger@users.sourceforge.net wrote:
> >
> > list_contains(PyListObject *a, PyObject *el)
> > {
> > ! int i, cmp;
> >
> > ! for (i = 0, cmp = 0 ; cmp == 0 && i < a->ob_size; ++i)
> > ! cmp = PyObject_RichCompareBool(el, PyList_GET_ITEM(a, i),
> > Py_EQ);
> > ! if (cmp > 0)
> > ! return 1;
> > ! if (cmp < 0)
> > ! return -1;
> > return 0;
> > }
>
> PyObject_RichCompareBool() returns the correct value:
> /* Return -1 if error; 1 if v op w; 0 if not (v op w). */
>
> So you could do:
>
> for (...)
> cmp = PyObject_RichCompareBool(...);
> return cmp;
>
> Neal