Why does 4 < '3' return True in Python 2?

穿精又带淫゛_ 提交于 2019-12-17 13:44:12

问题


Why does 4 < '3' return True in Python 2?

Is it because when I place single quotes around a number Python sees it as a string and strings are bigger than numbers?


回答1:


Yes, any number will be less than any string (including the empty string) in Python 2.

In Python 3, you can't make arbitrary comparisons. You'll get a TypeError.


From the link in eryksun's comment:

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);

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.




回答2:


From Python v2.7.2 documentation

Objects of different types except numbers are ordered by their type names; objects of the same types that don’t support proper comparison are ordered by their address.

When you order two strings or two numeric types the ordering is done in the expected way (lexicographic ordering for string, numeric ordering for integers).

When you order a string and an integer the type names are ordered. "str" is lexicographically after "int", "float", "long", "list", "bool", etc. However a tuple will order higher than a string because "tuple" > "str":

0 > 'hi'
False
[1, 2] > 'hi'
False
(1, 2) > 'hi'
True

also see comparison uses lexicographical ordering from docs.python.org

In Python 3.x the behaviour has been changed so that attempting to order an integer and a string will raise an error:

>>> '10' > 5
Traceback (most recent call last):
File "", line 1, in 
'10' > 5
TypeError: unorderable types: str() > int()



回答3:


The default comparison operation in cpython 2 is based on the memory address of the object in question. From type_richcompare() in python 2.7:

/* Compare addresses */
vv = (Py_uintptr_t)v;
ww = (Py_uintptr_t)w;
switch (op) {
case Py_LT: c = vv <  ww; break;
case Py_LE: c = vv <= ww; break;
case Py_EQ: c = vv == ww; break;
case Py_NE: c = vv != ww; break;
case Py_GT: c = vv >  ww; break;
case Py_GE: c = vv >= ww; break;
default:
    result = Py_NotImplemented;
    goto out;
}
result = c ? Py_True : Py_False;

This works really well for equality and inequality, but can be counter-intuitive for the ordering operations, so it has been changed for Python 3. Indeed, 2.7 will issue a warning for such usages when given the -3 flag.

To see the memory address of a given object, you can use the id() builtin function on it. It's only defined to return something unique for each individual object, but cpython uses the object's memory address as a convenient shortcut.

Small integers just happen to have smaller memory addresses than short strings, at least in certain python versions, probably due to the caching used by cpython to enhance performance.



来源:https://stackoverflow.com/questions/7969552/why-does-4-3-return-true-in-python-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!