cmp() on integers - is there guarantee of returning only +-1 or 0?

Paul Rubin http
Sun Mar 19 16:37:05 EST 2006


"Dmitry Anikin" <anikin#remove_this#@vstu.ru> writes:
> doc says that it must be > 0, or < 0, but it seems that
> it returns +1 or -1. Can it be reliably used to get the sign of x:
> cmp(x, 0) like pascal Sign() function does? 

The doc says (http://docs.python.org/lib/built-in-funcs.html):

     cmp(x,y)
         Compare the two objects x and y and return an integer according
         to the outcome. The return value is negative if x < y, zero if
         x == y and strictly positive if x > y.

> I mean, I'm pretty sure that it can be used, but is it mentioned
> somewhere in language spec, or it may be implementation defined?

The doc doesn't specify that it returns -1/0/+1 so by definition
it's implementation defined.

> If so, any other simple means of _reliably_ getting the sign?

Ehh, every way I see offhand is at least a little bit messy.



More information about the Python-list mailing list