What is being compared?

Tim Peters tim.one at home.com
Wed Mar 21 19:59:59 EST 2001


[Daniel Klein]
> Comparing a string to an integer...
>
> >>> 'a' < 1
> 0
>
> ...what is it actually comparing? Is it 'casting' anything?

No cast, but what is being compared isn't defined, just that cross-type
comparisons (except for cross-type all-numeric comparisons) are "consistent".
As a matter of implementation accident, CPython actually compares the string
names of the *types* in such cases, so the accident du jour is that
cmp(some_string, some_int) == cmp("string", "int"), which is greater than 0
(i.e., any string is "bigger than" any int -- today).

not-the-whole-truth-but-there's-not-much-future-seeking-truth-
    in-accidents-anyway-ly y'rs  - tim





More information about the Python-list mailing list