469,645 Members | 1,201 Online

# comparing strings and integers

By mistake I coded something like

print ("1" > 1)

and got the result "True". Comparing an integer and a string seems
meaningless to me, and I would prefer to have an exception thrown. Can
someone explain why Python allows comparisons between integers and
strings, and how it handles those cases? Why is "1" > 1?

Pychecker does not warn about the line of code above -- I wish it did.

In my code what I really intended was to convert the "1" to an int and
THEN do a comparison.
Jul 18 '05 #1
5 7276
without knowing any better, its possible "1" translates to 0x31

so it would become

0x31 > 1 ===> True....

David
http://cellphone.duneram.com/index.html

----- Original Message -----
From: <be*******@aol.com>
Newsgroups: comp.lang.python
To: <py*********@python.org>
Sent: Wednesday, May 19, 2004 5:26 PM
Subject: comparing strings and integers

By mistake I coded something like

print ("1" > 1)

and got the result "True". Comparing an integer and a string seems
meaningless to me, and I would prefer to have an exception thrown. Can
someone explain why Python allows comparisons between integers and
strings, and how it handles those cases? Why is "1" > 1?

Pychecker does not warn about the line of code above -- I wish it did.

In my code what I really intended was to convert the "1" to an int and
THEN do a comparison.

Jul 18 '05 #2
On Wednesday 19 May 2004 02:26 pm, be*******@aol.com wrote:
By mistake I coded something like

print ("1" > 1)

and got the result "True". Comparing an integer and a string seems
meaningless to me, and I would prefer to have an exception thrown. Can
someone explain why Python allows comparisons between integers and
strings, and how it handles those cases? Why is "1" > 1?

Pychecker does not warn about the line of code above -- I wish it did.

In my code what I really intended was to convert the "1" to an int and
THEN do a comparison. From the manual:

The operators <, >, ==, >=, <=, and != compare the values of two
objects. The objects need not have the same type. If both are
numbers, they are converted to a common type. Otherwise, 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. In the future, the comparison rules for objects of
different types are likely to change.)

Jul 18 '05 #3
On 19 May 2004 14:26:59 -0700
be*******@aol.com wrote:

#> By mistake I coded something like

#> print ("1" > 1)

#> and got the result "True". Comparing an integer and a string seems
#> meaningless to me, and I would prefer to have an exception thrown.
#> Can someone explain why Python allows comparisons between integers
#> and strings, and how it handles those cases? Why is "1" > 1?

From the Python Reference Manual, chapter 5.9 Comparisons:

"""

The operators <, >, ==, >=, <=, and != compare the values of two
objects. The objects need not have the same type. If both are numbers,
they are converted to a common type. Otherwise, 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.
In the future, the comparison rules for objects of different types are
likely to change.)

"""

#> Pychecker does not warn about the line of code above -- I wish it
#> did.

Good point - it probably would be nice if it did. I don't know if it
is possible to check for such things in the general case, though.

--
Best wishes,
Slawomir Nowaczyk
( Sl***************@cs.lth.se )

Never argue with an idiot: first he will drag you down to his level,
then he will beat you with experience.
Jul 18 '05 #4
Thanks to S. Nowaczyk and others for the helpful replies. If one wants

if (a > b)

to raise an exception when the comparison is not "sensible", maybe an
alternative way of writing it is

if ((a - b) > 0)

This should work when 'a' and 'b' are numerical and will fail if one
is numerical and the other is a string. A down side is that it does
not permit string comparisons, but I rarely compare strings except for
equality in my code.
Jul 18 '05 #5
be*******@aol.com wrote:
Thanks to S. Nowaczyk and others for the helpful replies. If one wants

if (a > b)

to raise an exception when the comparison is not "sensible", maybe an
alternative way of writing it is

if ((a - b) > 0)

This should work when 'a' and 'b' are numerical and will fail if one
is numerical and the other is a string. A down side is that it does
not permit string comparisons, but I rarely compare strings except for
equality in my code.

How do you define "sensible"?
Jul 18 '05 #6

### This discussion thread is closed

Replies have been disabled for this discussion.