By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,742 Members | 1,224 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,742 IT Pros & Developers. It's quick & easy.

comparing strings and integers

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
In article <30*************************@posting.google.com> ,
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.