我的cmp 老是报错

来源:9-1 什么是函数

慕侠0006521

2021-11-10 08:55

cmp(12,52)

Traceback (most recent call last):

  File "<pyshell#37>", line 1, in <module>

    cmp(12,52)

NameError: name 'cmp' is not defined


写回答 关注

1回答

  • hermaniu
    2021-11-12 16:31:12

    Python 3的版本中已经没有 cmp 函数,如果你需要实现比较功能,需要引入 operator 模块,适合任何对象,包含的方法有:

    operator.lt(a, b)operator.le(a, b)operator.eq(a, b)operator.ne(a, b)operator.ge(a, b)operator.gt(a, b)


Python3 入门教程(新版)

python3入门教程,让你快速入门并能编写简单的Python程序

163604 学习 · 1130 问题

查看课程

相似问题