comparing strings and ints
Fredrik Lundh
effbot at telia.com
Mon Apr 10 12:04:14 EDT 2000
Mark H.H. Montague wrote:
> Does anyone know why it is not a type error to compare a string and an
> int with '>' ? For example, I just spent over an hour debugging code
> that essentially did this:
>
> >>> 45 > '0'
> 0
>
> Of course, the '0' was a variable read in from a file, which I forgot
> to convert to a string. Bad old perl habits. But I'm having a hard
> time imagining a situation where someone would be intentionally and
> meaningfully comparing a string and an int.
>
> Any ideas?
why not read the fine documentation:
http://www.python.org/doc/current/ref/comparisons.html
which explains that:
Objects of different types always compare unequal,
and are ordered consistently but arbitrarily.
This unusual definition of comparison was used to
simplify the definition of operations like sorting and
the "in" and "not in" operators.
and goes on to say:
In the future, the comparison rules for objects of
different types are likely to change.
</F>
More information about the Python-list
mailing list