comparing strings and integers

B

beliavsky

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.
 
G

Gary Herron

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

Slawomir Nowaczyk

On 19 May 2004 14:26:59 -0700
(e-mail address removed) 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
( (e-mail address removed) )

Never argue with an idiot: first he will drag you down to his level,
then he will beat you with experience.
 
B

beliavsky

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.
 
R

Roy Smith

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"?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top