I can’t understand why Python doesn’t have a sign function. It already includes an abs builtin function (which I consider a natural counterpart to a sign function), but there’s no direct way to calculate the sign of a number.
For example, in Python 2.6, there’s even a copysign function in the math module, but still no Python sign function. Why create something like copysign(x, y) when you could simply define a sign function and derive copysign using abs(x) * sign(y)?
That approach would be much more intuitive: “x with the sign of y.” With copysign, you must remember if it means “x with the sign of y” or “y with the sign of x,” which adds unnecessary confusion.
Although you can implement a sign function using cmp(x, 0), it is far less readable. For a language like Python, which values simplicity and clarity, having a Python sign function would be a major benefit.
If I were designing Python, I’d prefer to have a built-in sign instead of cmp. If cmp(x, y) is needed, it could simply be derived as sign(x - y). This would make code more readable, as the sign function directly communicates that the result is positive, negative, or zero based on the comparison.
Of course, I understand cmp has its place, such as when sorting non-numeric data or ensuring a stable sort. But for numeric operations, I’ve found myself needing a Python sign function countless times in my seven years of using the language.
The absence of a built-in solution is puzzling, especially considering the inclusion of copysign, which is arguably less useful than a dedicated sign function.
So, why did the Python designers decide to exclude a built-in Python sign function? Am I missing a key reason behind this decision?