NameError: name 'cmp' is not defined 1. 这个错误意味着在当前的代码环境中,cmp函数没有被定义。 问题原因 在Python 3中,cmp函数已经被移除了。在早期的Python版本中,cmp函数用于比较两个对象的大小,并返回一个负整数、零或正整数来表示它们之间的相对顺序。然而,由于该函数并不符合Python中的惯例和一致性,它...
在Python 2.x中,cmp()函数的用法如下: a=1b=2print(cmp(a,b))# 输出结果为-1 1. 2. 3. 但是在Python 3.x中,如果我们直接使用cmp()函数会报错,提示"NameError: name ‘cmp’ is not defined",因为cmp()函数在Python 3.x中已经被废弃。 那么在Python 3.x中,我们应该如何替代cmp()函数呢?下面我...
NameError: name 'cmp' is not defined **报错原因:**因为python3.x中cmp函数去掉了,如果需要实现比较功能,那么可引入operator 模块,提供了6个比较运算符。gt lt ge eq le importoperator#首先要导入运算符模块operator#integersx,y = 100,200print("x:",x,", y:",y)print("operator.gt(x,y):", op...
我收到错误: Traceback (most recent call last): File "G:\Dropbox\Code\a = [1,2,3]", line 3, in <module> c = cmp(a,b) NameError: name 'cmp' is not defined [Finished in 0.1s] 原文由 BenFire 发布,翻译遵循 CC BY-SA 4.0 许可协议 pythonpython-3.x 有用关注收藏 回复 阅读34...
.py", line 75, in __lt__ return mycmp(self.obj, other.obj) < 0 File "/home/bioinfo5/miniconda3/lib/python3.6/site-packages/ete3/tools/ete_build_lib/scheduler.py", line 134, in sort_tasks prio_cmp = cmp(x_type_prio, y_type_prio) NameError: name 'cmp' is not defined ...
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)
>>>cmp(1,8) Traceback (most recent call last): File"<stdin>", line1,in<module> NameError: name'cmp'isnotdefined Python3 报错解决方法 1 python3.4.3的版本中已经没有cmp(x,y)函数,被operator模块代替,使用operator完美解决该报错!!!
cmp(x, y) Compare the...版本:该函数只有在python2中可用,而且在python2所有版本中都可用。但是在python3中该函数已经被删减掉。...>>> cmp(3,4) -1 >>> cmp(56,34) 1 >>> cmp(a,a) Traceback (most recent call last): File "", line...1, in NameError: name 'a' is not defined ...
i): self.i = i def __cmp__(self, other): return - c
Guido did change this on purpose, but it's still not guaranteed to stay this way across releases. I just added this blurb to the NEWS file (revision 1.123): The outcome of comparing non-numeric objects of differerent types is not defined by the language, other than that it's arbitrary ...