I just noticed that Python allows you to compare a string value with a number:
>>> '10' > 1000
True
>>> 'a' > 1000
True
>>> 'a' > -1000
True
Why is any string always greater than any number?
I just noticed that Python allows you to compare a string value with a number:
>>> '10' > 1000
True
>>> 'a' > 1000
True
>>> 'a' > -1000
True
Why is any string always greater than any number?
In Python 2, §5.3 - Comparisons says:
Objects of different types, except different numeric types and different string types, never compare equal; such objects are ordered consistently but arbitrarily (so that sorting a heterogeneous array yields a consistent result). Furthermore, some types (for example, file objects) support only a degenerate notion of comparison where any two objects of that type are unequal. Again, such objects are ordered arbitrarily but consistently. The
<
,<=
,>
and>=
operators will raise a TypeError exception when any operand is a complex number.
In Python 3, §4.3 - Comparisons says:
Objects of different types, except different numeric types, never compare equal. Furthermore, some types (for example, function objects) support only a degenerate notion of comparison where any two objects of that type are unequal. The
<
,<=
,>
and>=
operators will raise a TypeError exception when comparing a complex number with another built-in numeric type, when the objects are of different types that cannot be compared, or in other cases where there is no defined ordering.