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

后端 未结 3 1489
我寻月下人不归
我寻月下人不归 2020-12-03 21:01

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 stri

相关标签:
3条回答
  • 2020-12-03 21:14

    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()
    
    0 讨论(0)
  • 2020-12-03 21:16

    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.

    0 讨论(0)
  • 2020-12-03 21:38

    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.

    0 讨论(0)
提交回复
热议问题