Advertisements
Advertisements
Question
Explain in detail about cmp() function.
Explain
Advertisements
Solution
cmp()was a Python 2 function used to compare two objects, including dictionaries and tuples.- Its historical form was
cmp(x, y). - It returned:
0whenxandywere equal.- A positive value when
xwas greater thany. - A negative value when
xwas less thany.
- From the source examples:
cmp(month, m)returns0when both dictionaries are the same;cmp(m, n)returns1; andcmp(n, m)returns-1. cmp()was removed in Python 3. Use comparison operators instead:x == yfor equality.x > yto check whetherxis greater thany.x < yto check whetherxis less thany.
- A Python 3 equivalent returning
-1,0, or1is:
result = (x > y) - (x < y)
shaalaa.com
Is there an error in this question or solution?
