Missing_cmp (was Re: '==' vs. 'is' behavior)

Kent Polk kent at tiamat.goathill.org
Tue Nov 30 23:03:06 CET 1999

On 30 Nov 1999 15:05:09 -0500, Andrew M. Kuchling wrote:

) At the time I thought the Python comparison was a bit
>odd, and didn't really see the point.  Python is more like a
>collection of tiny interacting modules; no one module is very
>complicated, but put them together and they intermesh and interweave
>in a beautiful way over time. [1] Over time the modules fall together
>in slightly different patterns, and, though the difference between
>iterations is slight, the resulting shift can completely change the
>effect of the whole piece.

Speaking of weaving Python module comparisons together...

The module Missing, which provides a placeholder mechanism for
missing data (one of Jim's ExtensionModules for Zope) has some
interesting cmp behavior:

>>> import Missing
>>> b=[5,Missing.Value,0,-3,'c',Missing.Value,0,4,'a',Missing.Value,
>>> b.sort()
>>> b
[-3, 5, Missing.Value, 0, Missing.Value, 0, 4, Missing.Value,
   Missing.Value, 6, 'a', 'b', 'c']

As you can see, inserting a Missing.Value before a zero in a list
causes problems with numeric compares.

Note that Missing.Values currently evaluate to 0, and the current
compare method for Missing objects is:

static int
Missing_cmp(Missing *m1, Missing *m2)
  return m1->ob_type != m2->ob_type ;

which always returns 1 on failure. This leads to evaluating 5 as
less than Missing.Value under certain circumstances.  A better
solution would be to always return -1 on failure so that Missing.Value
evaluates to less than any object greater than zero, as in:

[-3, Missing.Value, Missing.Value, Missing.Value, 0, 0, 4, 5, 5,
'a', 'c']

My question is whether this is the desired behavior or not...
Should Missing.Value really be ordered as equal to zero or should
it be less than other object types?  I suspect that if it evaluates
to a non-zero value, problems might ensue with how people handle
no data, so I suppose this should be sufficient behavior. However,
is there any way for Missing.Value to evaluate to zero, yet have
the compare function order Missing.Values as less than other object
types?  (ha)


More information about the Python-list mailing list