In Python 2, it was possible to compare objects of different types such as int
to str
by having an implicit comparison of the text string of types (that is, in lexicographic order, string 'int'
is less than string 'str'
and string 'list'
is less than string 'tuple'
).
Hence, in Python 2, 5 < 'hello'
returns True
. One can read more about why this was allowed in answer to Why is ''>0 True in Python?.
In Python 3, this raises builtins.TypeError: unorderable types: int() < str()
exception.
This web page says
The strict approach to comparing in Python 3 makes it generally impossible to compare different types of objects.
Does it mean there are some built-in types or special cases where it would be possible for any built-in types to be compared without causing TypeError
? I am not talking about custom types where the necessary dunder methods are implemented to properly support comparison.