python3.2.2版本中的cmp()函数

>>>a,b=1,2>>>a,b(1,2)>>>cmp(a,b)Traceback(mostrecentcalllast):File"<interactiveinput>... >>> a,b = 1,2
>>> a,b
(1, 2)
>>> cmp(a,b)
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
NameError: name 'cmp' is not defined
>>>

为啥会出现如下情况呢?是不是3.2.2中没有这个函数了,有哪个函数代替cmp()内建函数呀?谢谢!
展开
 我来答
longinus_dean
推荐于2018-02-27 · TA获得超过2282个赞
知道小有建树答主
回答量:1097
采纳率:50%
帮助的人:1552万
展开全部
3开始没这个函数了,官方文档是这么写的

The cmp() function should be treated as gone, and the __cmp__() special method is no longer supported. Use __lt__() for sorting, __eq__() with __hash__(), and other rich comparisons as needed. (If you really need the cmp() functionality, you could use the expression (a > b) - (a < b) as the equivalent for cmp(a, b).)

大意就是cmp()函数已经“离开”了,如果你真的需要cmp()函数,你可以用表达式(a > b) - (a < b)代替cmp(a,b)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
?>

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式