In python 3, int(50)<\'2\'
causes a TypeError
, and well it should. In python 2.x, however, int(50)<\'2\'
returns True
It works like this1.
>>> float() == long() == int() < dict() < list() < str() < tuple()
True
Numbers compare as less than containers. Numeric types are converted to a common type and compared based on their numeric value. Containers are compared by the alphabetic value of their names.2
From the docs:
CPython implementation detail: 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.
Objects of different builtin types compare alphabetically by the name of their type .int
starts with an 'i' and str
starts with an s
so any int
is less than any str
.
In response to the comment about long < int
>>> int < long
True
You probably meant values of those types though, in which case the numeric comparison applies.
1 This is all on Python 2.6.5
2 Thank to kRON for clearing this up for me. I'd never thought to compare a number to a dict
before and comparison of numbers is one of those things that's so obvious that it's easy to overlook.