Why Python doesn't have a sign function?

I can’t understand why Python doesn’t include a sign function. It has a built-in abs function, which I consider to be a sister function to sign, but no dedicated sign function exists.

In Python 2.6, there’s even a copysign function in the math module, but still no sign. Why would we have a copysign(x, y) function when we could simply write sign(x) * abs(y)? The latter would be much clearer: the sign of x with the magnitude of y. With copysign, though, you have to remember whether it’s x with the sign of y or vice versa!

While sign(x) doesn’t provide anything more than cmp(x, 0), it would certainly be more readable, and given Python’s emphasis on readability, this would be a useful addition. If I were a Python designer, I might have opted for a sign function instead of cmp. For example, sign(x - y) could be used to compare two values, providing a more intuitive and readable approach.

So the question is: why did the Python designers decide to leave out the sign function? Why prioritize copysign and not its more fundamental counterpart, sign?

Am I missing something here?

Would a sign function have been more useful than copysign, and why was copysign prioritized over it in the math module?

Python prioritizes simplicity and readability, and the designers might have felt that a sign function wasn’t necessary because cmp(x, 0) or simple boolean checks like x > 0 could easily handle the functionality. This approach keeps the language minimalistic, avoiding adding functions that could be easily replaced by existing features. Instead, the math.copysign function, which is more specialized, was prioritized to provide precise control over the sign of a number.

Historical context and language evolution: The cmp() function used to be part of Python’s design, and when cmp was removed in Python 3, the focus shifted to simpler, more intuitive comparisons. The need for a sign function likely seemed redundant at the time because Python’s existing functions could suffice.

The inclusion of copysign addresses the need to modify the sign of numbers while keeping the magnitude intact, which is a more specific and less general operation than simply determining the sign of a number.

Specialized use cases: copysign was introduced in the math module to handle specific edge cases when working with floating-point values, such as when the sign of a number needs to be modified independently of its magnitude.

In contrast, sign would be a more general-purpose function, which might explain why Python chose to stick with the more specific utility provided by copysign. While sign could indeed be useful, its implementation is straightforward and can be manually mimicked with cmp or simple boolean expressions.