为了账号安全,请及时绑定邮箱和手机立即绑定

为啥会出现如下情况呢?是不是3.2.2中没有这个函数了,有哪个函数代替cmp()内建函数呀?

为啥会出现如下情况呢?是不是3.2.2中没有这个函数了,有哪个函数代替cmp()内建函数呀?

HUWWW 2022-11-18 15:11:28
>>> 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>>> 
查看完整描述

1 回答

?
慕婉清6462132

TA贡献1804条经验 获得超2个赞

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)

查看完整回答
反对 回复 2022-11-22
  • 1 回答
  • 0 关注
  • 106 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号