comparing strings and ints

Gordon McMillan gmcm at hypernet.com
Tue Apr 11 15:26:52 EDT 2000


Randall Hopper wrote:

> Fredrik Lundh:
>  |which explains that:
>  |
>  |    Objects of different types always compare unequal,
>  |    and are ordered consistently but arbitrarily.
> 
> Reminds me of qsort() core dumps because the compiled C library compared
> NaNs and Infs arbitrarily.
> 
> I can see 45 == '45' being false, and 45 != '45' being true.  But it seems
> to me that not throwing an exception for attempts at an ordered comparison
> with logical ordering operators (<, <=, etc.) on objects of different
> primitive types (e.g. 45 < '45', 45 > '45') only lets bugs pass through.
> 
> Is there a case where this could be useful (with its undefined behavior)?
> I believe that is the root of the question.

I'd happily bet your wife and firstborn child that there's code that
relies on it <wink>, if only because after sorting a list, all the ints
will end up in a clump, and all the strings in another, etc. 


- Gordon




More information about the Python-list mailing list