How to use cmp() in Python 3?

后端 未结 5 1494
甜味超标
甜味超标 2020-12-01 09:18

I cannot get the command cmp() to work.

Here is the code:

a = [1,2,3]
b = [1,2,3]
c = cmp(a,b)
print (c)

I am getting

5条回答
  •  醉酒成梦
    2020-12-01 09:42

    If a or b is a class object, then the above answers will have the compilation error as below: For example: a is Class Clock: File "01_ClockClass_lab16.py", line 14, in cmp return (a > b) - (a < b) TypeError: '>' not supported between instances of 'Clock' and 'Clock'

    Change the type with int() to remove the error:

    def cmp(a, b): return (int(a) > int(b)) - (int(a) < int(b))

提交回复
热议问题