作者:素生密言
[En]
Author: sushengmiyan
在python2中我们经常会使用cmp函数来比较一些东西,但是在python3中,你再来使用这个函数的时候,发现就报错了,提示找不到这个函数,这是为啥呢?嗯,新版的python已经舍弃之。
在python3中使用时报错信息如下:
Syntax Error: if cmp(os.getcwd(), os.path.dirname(os.sys.argv[0])) <> 0:: Test.py, line 34055
提示找不到cmp函数,如何在python3中使用该函数?
[En]
Prompt that the cmp function can not be found, so how can I use this function in python3?
看python的帮助文档,在oprater这个模块中有了这么几个函数
operator.lt(a, b)
operator.le(a, b)
operator.eq(a, b)
operator.ne(a, b)
operator.ge(a, b)
operator.gt(a, b)
operator.__lt__(a, b)
operator.__le__(a, b)
operator.__eq__(a, b)
operator.__ne__(a, b)
operator.__ge__(a, b)
operator.__gt__(a, b)
这些函数用于取代以前的cmp。对于那些以前使用过CMP的人,我们将在未来替换这些函数。
[En]
These functions are used to replace the previous cmp. For those of you who used cmp before, we will replace these functions in the future.
让我们简要地解释一下这些函数的含义。
[En]
Let’s briefly explain the meaning of these functions.
lt(a, b) 相当于 a < b
le(a,b) 相当于 a
eq(a,b) 相当于 a == b
ne(a,b) 相当于 a != b
gt(a,b) 相当于 a > b
该函数的返回值不是布尔值。需要注意的是,它与返回数值的cmp相同。
[En]
The return value of the function is not Boolean. It should be noted that it is the same as cmp, which returns a numerical value.
Original: https://www.cnblogs.com/muyuge/p/6152539.html
Author: 木鱼哥
Title: python3中替换python2中cmp函数的新函数分析(lt、le、eq、ne、ge、gt)
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/6646/
转载文章受原作者版权保护。转载请注明原作者出处!