![]() |
NameError: name 'cmp' is not defined - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: NameError: name 'cmp' is not defined (/thread-4812.html) |
NameError: name 'cmp' is not defined - rajeev1729 - Sep-10-2017 list1, list2 = [123, 'xyz'], [456, 'abc'] print cmp(list1, list2) print cmp(list2, list1) list3 = list2 + [786]; print cmp(list2, list3)Traceback (most recent call last): File "C:/python/program/listcmp.py", line 6, in <module> print (cmp(list1, list2)) NameError: name 'cmp' is not defined RE: NameError: name 'cmp' is not defined - ichabod801 - Sep-10-2017 The cmp built-in function is no longer available in Python. The code you have appears to be Python 2.x code, and you seem to be running it in Python 3.x. You need to run it in Python 2.x for it to work. If you are just learning Python, I suggest switching to learning 3.x (3.6 is the latest version). There are plenty of resources out there for learning Python 3.x. RE: NameError: name 'cmp' is not defined - rajeev1729 - Sep-10-2017 so what is substitution for cmp in python 3.6. i am working on python 3.6. RE: NameError: name 'cmp' is not defined - snippsat - Sep-10-2017 As mentioned so do not cmp exist in Python 3.If you really want it,you could define it yourself. Or think about whether it's actually the best way to do, whatever you up to do. def cmp(a, b): return (a > b) - (a < b) list1, list2 = [123, 'xyz'], [456, 'abc'] print(cmp(list1, list2)) print(cmp(list2, list1)) list3 = list2 + [786]; print(cmp(list2, list3))
|