According to the doc and this tutorial,
cmp() returns -1 if x < y
and
cmp() returns 0 if x == y
and
cmp() returns 1 if x > y
The tutorial also said that
cmp() returns the sign of the difference of two numbers
I don't really get what sign of the difference of two numbers means. Doesn't that mean that it returns a value when the sign of numbers aren't equal? Since...
cmp(80, 100) : -1 # both have positive sign. cmp(180, 100) : 1 # both also have positive sign. cmp(-80, 100) : -1 cmp(80, -100) : 1
**Note: code from the tutorial.*
Despite my confusion in sign differences, I can't really think of why do we need a built-in function to return a value of -1 when x < y.
Isn't the function cmp( )
easily implemented ? Is there any reason why Python creators keep cmp( )
function, or is there any hidden usage of this Python's cmp( )
function ?
A comprehensive metabolic panel (CMP) is a test that measures 14 different substances in your blood. It provides important information about your body's chemical balance and metabolism. Metabolism is the process of how the body uses food and energy.
What Is a Comprehensive Metabolic Panel (CMP)? The comprehensive metabolic panel (CMP) is a blood test that gives doctors information about the body's fluid balance, levels of electrolytes like sodium and potassium, and how well the kidneys and liver are working.
The CMP can be used to diagnose diabetes, high blood pressure, kidney disease, liver disease, hypertension, or it is often just used as part of a regular health examination.
Most people get a CMP as part of their yearly checkup. The doctor might also want one to check you for problems, keep track of any chronic conditions you have, or make sure certain medications aren't hurting your liver or kidneys.
Why cmp( ) is useful?
It isn't very useful, which is why it was deprecated (the builtin cmp
is gone and builtin sorts no longer accept one in Python 3). Rich comparison methods supplanted it:
object.__lt__(self, other) object.__le__(self, other) object.__eq__(self, other) object.__ne__(self, other) object.__gt__(self, other) object.__ge__(self, other)
This allows the <
symbol (and other symbols) to be overloaded comparison operators, enabling, for example, subset and superset comparisons of set objects.
>>> set('abc') < set('cba') False >>> set('abc') <= set('cba') True >>> set('abc') == set('cba') True >>> set('abc') >= set('cba') True >>> set('abc') > set('cba') False
while it could enable the above, cmp
wouldn't allow the following:
>>> set('abc') == set('bcd') False >>> set('abc') >= set('bcd') False >>> set('abc') <= set('bcd') False
cmp
Here's an interesting usage which uses its result as an index (it returns -1 if the first is less than the second, 0 if equal, and 1 if greater than):
def cmp_to_symbol(val, other_val): '''returns the symbol representing the relationship between two values''' return '=><'[cmp(val, other_val)] >>> cmp_to_symbol(0, 1) '<' >>> cmp_to_symbol(1, 1) '=' >>> cmp_to_symbol(1, 0) '>'
According to the docs, you should treat cmp as if it wasn't there:
https://docs.python.org/3/whatsnew/3.0.html#ordering-comparisons
cmp
removed, equivalent operationBut you can use this as the equivalent:
(a > b) - (a < b)
in our little toy function, that's this:
def cmp_to_symbol(val, other_val): '''returns the symbol representing the relationship between two values''' return '=><'[(val > other_val) - (val < other_val)]
I don't really get what does it mean sign of the difference of two numbers.
This means: take the difference, and then the sign of that difference. For example, if x
and y
are two numbers:
x < y
=> x - y < 0
and the function returns -1.x == y
=> x - y == 0
and the function returns 0.x > y
=> x - y > 0
and the function returns 1.For more information on three-way comparisons, see Wikipedia.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With