Re: Simple Python question for some

Discussion in 'Python' started by Chris Rebert, Oct 29, 2012.

  1. Chris Rebert

    Chris Rebert Guest

    On Sun, Oct 28, 2012 at 4:51 PM, Mark L. Hotz <> wrote:
    > I have what I think should be a relatively simple question for someone who
    > is knowledgeable about Python.
    >
    > At the IDLE prompt, when I enter “b†> 99, it responds True. In fact, it
    > doesn’t matter which number is entered here, “b†is always greater (e.g. “bâ€
    >> 1 == True; “b†> 100000 == True, or “b†< 99 = False).

    >
    > Why is this true?


    Per http://docs.python.org/2/library/stdtypes.html#comparisons :
    "Objects of different types, except different numeric types and
    different string types, […] are ordered consistently but arbitrarily
    (so that sorting a heterogeneous array yields a consistent result)."
    Note that the "except" part just means that, e.g. floats and ints can
    be compared with each other, and Unicode and byte strings can be
    compared with each other. It does NOT mean that numbers and strings
    can be meaningfully compared with each other.

    This is fixed in Python 3, where such nonsensical comparisons will
    instead raise TypeError.

    > If I use ord(“bâ€) it returns 98, so Python cannot be
    > using the ASCII or Unicode value when interpreting “b†> 99.


    It has nothing to do with implicit casting between strings and numbers
    (which, as a general rule, Python does not do).

    >From the same linked section as before:

    "CPython implementation detail: Objects of [incompatible types] are
    ordered by their type names"

    So ints come before strs because "int" comes before "str" lexicographically..

    Cheers,
    Chris
     
    Chris Rebert, Oct 29, 2012
    #1
    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. Eric
    Replies:
    2
    Views:
    1,568
    Tommy
    Feb 13, 2004
  2. Mark Lawrence

    Re: Simple Python question for some

    Mark Lawrence, Oct 29, 2012, in forum: Python
    Replies:
    0
    Views:
    139
    Mark Lawrence
    Oct 29, 2012
  3. Chris Angelico

    Re: Simple Python question for some

    Chris Angelico, Oct 29, 2012, in forum: Python
    Replies:
    0
    Views:
    176
    Chris Angelico
    Oct 29, 2012
  4. Evan Driscoll

    Re: Re: Simple Python question for some

    Evan Driscoll, Oct 29, 2012, in forum: Python
    Replies:
    0
    Views:
    158
    Evan Driscoll
    Oct 29, 2012
  5. Chris Angelico

    Re: Re: Simple Python question for some

    Chris Angelico, Oct 29, 2012, in forum: Python
    Replies:
    0
    Views:
    173
    Chris Angelico
    Oct 29, 2012
Loading...

Share This Page