[Tutor] why can't I find a function that givesme the sign of aninteger?
kent37 at tds.net
Fri Jan 27 12:01:10 CET 2006
Alan Gauld wrote:
>>Eh you mean to say that in next Python versions someone could decide
>>to change cmp(x,0) to another meaning? I bet my countryman (I'm from
>>Holland too ;-) ) will veto that! Or else I'll pay him a visit :D
> Its not another meaning, its the current meaning.
> Kent is just pointing out that while the default cmp currently
> returns -1,0,1
> there is nothing to stop a user defined cmp fom returning any negative or
> positive number instead of -1,1. And cmp() calls any user defined cmp
> under the hood.
> In theory the standard cmp could be changed in future although its
> So while it is a nice trick it cannot be relied upon since it depends on a
> detail of implementation. In practice I suspect you are fairly safe :-)
The test suite does specifically test for -1, 0 and 1 as the result of
self.assertEqual(cmp(-1, 1), -1)
self.assertEqual(cmp(1, -1), 1)
self.assertEqual(cmp(1, 1), 0)
so this behaviour will probably be consistent in different Python
versions. Jython for example tries to pass the standard tests and I
imagine IronPython does as well, and of course (C)Python.
More information about the Tutor