comparing strings and integers

Discussion in 'Python' started by beliavsky@aol.com, May 19, 2004.

  1. Guest

    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.
     
    , May 19, 2004
    #1
    1. Advertising

  2. 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: <>
    Newsgroups: comp.lang.python
    To: <>
    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.
     
    David Stockwell, May 19, 2004
    #2
    1. Advertising

  3. Gary Herron Guest

    On Wednesday 19 May 2004 02:26 pm, 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.)
     
    Gary Herron, May 19, 2004
    #3
  4. On 19 May 2004 14:26:59 -0700
    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
    ( )

    Never argue with an idiot: first he will drag you down to his level,
    then he will beat you with experience.
     
    Slawomir Nowaczyk, May 19, 2004
    #4
  5. Guest

    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.
     
    , May 20, 2004
    #5
  6. Roy Smith Guest

    In article <>,
    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"?
     
    Roy Smith, May 20, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nag
    Replies:
    3
    Views:
    535
  2. Nicholas

    Comparing char* with integers and characters

    Nicholas, Sep 9, 2003, in forum: C Programming
    Replies:
    13
    Views:
    607
    Nicholas
    Sep 9, 2003
  3. Rick

    Comparing strings from within strings

    Rick, Oct 21, 2003, in forum: C Programming
    Replies:
    3
    Views:
    386
    Irrwahn Grausewitz
    Oct 21, 2003
  4. Joe Van Dyk

    comparing signed and unsigned integers

    Joe Van Dyk, Jun 23, 2006, in forum: C Programming
    Replies:
    3
    Views:
    3,902
    Tom St Denis
    Jun 25, 2006
  5. Replies:
    11
    Views:
    568
    Patricia Shanahan
    Nov 20, 2007
Loading...

Share This Page