How does the max function work when items in a list are not of the same type?
For example the following code returns [1,’3′]
max([1,52,53],[1,'3']) => [1,'3']
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
In Python2, the default comparison for objects of different types is to compare using the id of their types (obtained by casting the object pointers to integers). Here a link to the source: http://hg.python.org/cpython/file/2.7/Objects/object.c#l757
On my build, here is the ordering of types:
Numbers (except for complex) have compare methods that allow cross type comparison based on numeric value (i.e. a float can be compared with an int).
The None object is special. It compares less than everything else.
To see it all put together, use sorted to see the ordering: