I can't understand why python has not a sign()
function. It has an abs()
builtin (which I consider sign()
's sister), but no sign.
In python 2.6 there is even a copysign()
function (in math), but no sign. Why bother to write a copysign(x,y)
when you could just write a sign()
and then get the copysign()
directly from abs(x) * sign(y)
? The latter would be much more clear: x with the sign of y, whereas with copysign you have to remember if it's x with the sign of y or y with the sign of x!
Obviously sign(x)
does not provide anything more than cmp(x,0)
, but it would be much more readable that this too (and for a greatly readable language like python, this would have been a big plus).
If I were a python designer, I would been the other way arond: no cmp()
builtin, but a sign()
. When you need cmp(x,y)
, you could just do a sign(x-y)
(or, even better for non-numerical stuff, just a x>y - of course this should have required sorted()
accepting a boolean instead of an integer comparator). This would also be more clear: positive when x>y
(whereas with cmp()
you have to remember the convention positive when the first is bigger, but it could be the other way around). Of course cmp()
makes sense in its own for other reasons (e.g. when sorting non-numerical things, or if you want the sort to be stable, which is not possible using with simply a boolean)
So, the question is: why python designer(s) decided to leave the sign()
function out of the language? Why in the heck bothering about copysign()
and not its parent sign()
???!
Am I missing something?
EDIT - after Peter Hansen comment. Fair enough that you didn't use it, but you didn't say what you use python for. In 7 years that I use python, I needed it countless times, and the last is the straw that broke the camel's back!
Yes, you can pass cmp around, but 90% of the times that I needed to pass it was in an idiom like
lambda x,y: cmp(score(x),score(y)
that would have worked with sign just fine.
Finally, I hope you agree that sign
would be more useful than copysign
, so even if I bought your view, why bother about defining that in math, instead of sign? How can copysign be so much useful than sign?