In Python 2, numeric values ββare always sorted before strings and almost all other types:
>>> sorted(['a', 5]) [5, 'a']
Then the numbers count as fewer lines. When using max() this means that the string is selected above the number.
These numbers are less than an arbitrary choice of implementation. See Comparative Documentation :
The operators < , > , == , >= , <= and != Compare the values ββof two objects. Objects must not have the same type. If both are numbers, they are converted to a common type. Otherwise, objects of different types are always compared unevenly and ordered sequentially, but arbitrarily.
My bold accent.
Python 2 tried very hard to sort heterogeneous types, which caused a lot of debugging difficulties, such as programmers trying to compare integers with strings and get unexpected results. Python 3 fixed this error; instead you get a TypeError :
>>> max(5, 'a') Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unorderable types: str() > int()
I wrote rules of order elsewhere and even reimplemented Python 2 rules for Python 3 if you really wanted them back.
source share