为什么4 < '3'在Python 2中返回True? [英] Why does 4 < '3' return True in Python 2?
本文介绍了为什么4 < '3'在Python 2中返回True?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为什么 4< '3'
return True
在Python 2中?
Why does 4 < '3'
return True
in Python 2?
推荐答案
是的,任何数字都会少于数字比如在Python 2中的任何字符串(包括空字符串)。
Yes, any number will be less than any string (including the empty string) in Python 2.
在Python 3中,你不能进行任意比较。 您会得到 TypeError
。
In Python 3, you can't make arbitrary comparisons. You'll get a TypeError
.
if (PyNumber_Check(v))
vname = "";
else
vname = v->ob_type->tp_name;
if (PyNumber_Check(w))
wname = "";
else
wname = w->ob_type->tp_name;
c = strcmp(vname, wname);
因此至少在CPython 2.x的最新版本中,类型名称与空字符串而不是任何数字类型的类型名称。
So at least in recent versions of CPython 2.x, type names are compared, with an empty string used instead of the type name for any numeric type.
这篇关于为什么4 < '3'在Python 2中返回True?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文