Back

Explore Courses Blog Tutorials Interview Questions
0 votes
3 views
in Python by (16.4k points)

I don't know, why I'm not getting the command cmp() to work

Look at the code:

a = [1,2,3]

b = [1,2,3]

c = cmp(a,b)

print (c)

But I'm getting this kind of error:

Traceback (most recent call last):

  File "G:\Dropbox\Code\a = [1,2,3]", line 3, in <module>

    c = cmp(a,b)

 NameError: name 'cmp' is not defined

[Finished in 0.1s]

1 Answer

0 votes
by (26.4k points)

As referenced in the remarks, cmp doesn't exist in Python 3. In the event that you truly need it, you could characterize it yourself: 

def cmp(a, b):

    return (a > b) - (a < b) 

which is taken from the first What's New In Python 3.0. It's quite uncommon - however not unfathomable - that it's truly required, however, so you should consider whether it's really the most ideal approach to do whatever it is you're doing.

Join the python training course and gain more knowledge in python

Browse Categories

...