Python 3.0, 'Hello' < 42

Scott David Daniels Scott.Daniels at Acm.Org
Wed Jan 28 19:33:04 CET 2009

towitowi at wrote:
> Hello,
> In the 3.0 changes list there is mentioned that "<"-compares are not
> supported anymore if the compared types are different (except
> numbers). Like
>   42 < "Hello"
> did return True of False depending on the implementation but is now a
> TypeError.
> But the document also mentions that the result is not strictly
> "undefined" but "reproducable undetermined". Meaning, that on a given
> machine with a given python implementation result will always be the
> same. But on another machine, or with another python implementation,
> it might give a different result.
> My question now is, why this is? E.g, Is there an integer large enough
> so that the result changes? What circumstances influence the result?

The ordering of ints and strings may vary between python builds, but
_any_ strings will have the same relationship to _any_ integers.
That is, (str(x) < int(y)) == (str(y) < int(x))  for any x and y that
don't cause exceptions.  The types are compared before the values for
sufficiently disparate types without accomodation in the comparison
methods.  This means the 2.X python series can sort more lists, but
the sort order is not a particularly useful one (except for the fact
that there is an order).

--Scott David Daniels
Scott.Daniels at Acm.Org

More information about the Python-list mailing list