Python Forum
NameError: name 'cmp' is not defined
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
NameError: name 'cmp' is not defined
#1
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
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
so what is substitution for cmp in python 3.6. i am working on python 3.6.
Reply
#4
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))
Output:
-1 1 -1
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I'm getting a NameError: ...not defined. vonArre 2 283 Mar-24-2024, 10:25 PM
Last Post: vonArre
  Getting NameError for a function that is defined JonWayn 2 1,106 Dec-11-2022, 01:53 PM
Last Post: JonWayn
Question Help with function - encryption - messages - NameError: name 'message' is not defined MrKnd94 4 2,891 Nov-11-2022, 09:03 PM
Last Post: deanhystad
  [split] NameError: name 'csvwriter' is not defined. Did you mean: 'writer'? cathy12 4 3,326 Sep-01-2022, 07:41 PM
Last Post: deanhystad
  NameError: name ‘app_ctrl’ is not defined 3lnyn0 0 1,515 Jul-04-2022, 08:08 PM
Last Post: 3lnyn0
  NameError: name 'hash_value_x_t' is not defined Anldra12 5 1,920 May-13-2022, 03:37 PM
Last Post: deanhystad
  NameError: name 'cross_validation' is not defined tmhsa 6 13,352 Jan-17-2022, 09:53 PM
Last Post: TropicalHeat
  NameError: name “x” is not defined ... even though x is defined campjaybellson 7 14,981 Oct-20-2021, 05:39 PM
Last Post: deanhystad
  NameError: name 'Particle' is not defined in Pygame drunkenneo 4 3,389 Aug-15-2021, 06:12 PM
Last Post: bowlofred
  NameError: name 'u1' is not defined (on parser code Python) Melcu54 1 2,887 Jul-26-2021, 04:36 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020