maximum = max(1, 1.25, 3.14, 'a', 1000)- why is it giving'a'as the answer? Shouldn't'a'get converted to ASCII and be checked?maximum = max(1, 2.15, "hello")gives"hello"as answer. How does this answer come?
2 Answers
From the documentation -
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
Hence str is always greater than int .
Some more examples -
>>> class test:
... pass
...
>>> t = test()
>>> 'a' > 5
True
>>> t > 'a'
False
>>> type(t)
<type 'instance'>
>>> t > 10
False
>>> type(True)
<type 'bool'>
>>> True > 100
False
>>> False > 100
False
Please note the type name of test class' object is instance that is why t > 5 is False .
4 Comments
2016rshah
Out of curiosity, what makes
str greater than int? It isn't alphabetical so how do they order the type names?jonrsharpe
@2016rshah what has brought you to the conclusion that
'str' > 'int' isn't alphabetical?Anand S Kumar
They are alphaetical, where did you see its not alphabetical? For object of a custom class, the type is
instance .2016rshah
Oh wait never mind I am not entirely sure what brought me to that conclusion. Silly mistake, sorry. Should I delete my comment?