What is being compared?

Michael Hudson mwh21 at cam.ac.uk
Thu Mar 22 07:30:05 EST 2001


"Tim Peters" <tim.one at home.com> writes:

> [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).

Well, apart from the fact that numeric types get a name of "" in such
considerations, so that you don't get absurdities like

(1 < [] < 1L) == 1

just because "int" < "list" < "long".

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

Quite.  Relying on calling .sort() on a list to get all the numbers to
the front is probably ill-advised (even before complex numbers started
throwing exceptions on comparisons!).

Cheers,
M.

-- 
  If your telephone company installs a system in the woods with no
  one around to see them, do they still get it wrong?
                                 -- Robert Moir, alt.sysadmin.recovery



More information about the Python-list mailing list