I just don't get it

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Eduardo Elgueta

    I just don't get it

    Hi All,

    Would anyone be so kind to explain this?

    -- begin --[color=blue][color=green][color=darkred]
    >>> (176L,) > 300[/color][/color][/color]
    True
    --end--

    (Python2.3)

    TIA.

    Ed.
  • Matt Gerrans

    #2
    Re: I just don't get it

    Try comparing apples and apples:
    [color=blue][color=green][color=darkred]
    >>> (176L,) > (300,)[/color][/color][/color]
    False


    Comment

    • John Hazen

      #3
      Re: I just don't get it

      * Eduardo Elgueta <eelgueta@navix .cl> [2004-02-27 16:18]:[color=blue]
      > Hi All,
      >
      > Would anyone be so kind to explain this?
      >
      > -- begin --[color=green][color=darkred]
      > >>> (176L,) > 300[/color][/color]
      > True
      > --end--[/color]

      Comparing objects of different types (tuple and integer in your
      example), while deterministic within a specific python version, puts
      them in an arbitrary order.
      [color=blue][color=green][color=darkred]
      >>> () > 300[/color][/color][/color]
      True[color=blue][color=green][color=darkred]
      >>> () > [][/color][/color][/color]
      True[color=blue][color=green][color=darkred]
      >>> [] > ()[/color][/color][/color]
      False[color=blue][color=green][color=darkred]
      >>> 'a' > 300[/color][/color][/color]
      True[color=blue][color=green][color=darkred]
      >>> 'a' > ()[/color][/color][/color]
      False[color=blue][color=green][color=darkred]
      >>> () > 'a' > 300[/color][/color][/color]
      True[color=blue][color=green][color=darkred]
      >>> [] > 'a'[/color][/color][/color]
      False[color=blue][color=green][color=darkred]
      >>> [] > 300[/color][/color][/color]
      True[color=blue][color=green][color=darkred]
      >>> () > 'a' > [] > 300[/color][/color][/color]
      True


      So, you probably don't want to be comparing disparate types and
      attaching any meaning to the result.

      If you wanted to compare the two numbers in your example, you would want
      to index the tuple to get at the value inside it:
      [color=blue][color=green][color=darkred]
      >>> (176L,) > 300[/color][/color][/color]
      True[color=blue][color=green][color=darkred]
      >>> (176L,)[0] > 300[/color][/color][/color]
      False


      HTH-

      John
      < my_first_name AT my_last_name DOT net >

      Comment

      • Stephen Horne

        #4
        Re: I just don't get it

        On 27 Feb 2004 15:16:32 -0800, eelgueta@navix. cl (Eduardo Elgueta)
        wrote:
        [color=blue]
        >Hi All,
        >
        >Would anyone be so kind to explain this?
        >
        >-- begin --[color=green][color=darkred]
        >>>> (176L,) > 300[/color][/color]
        >True
        >--end--
        >
        >(Python2.3)
        >
        >TIA.
        >
        >Ed.[/color]

        Those are different data types - you are comparing a tuple with a
        number. When different data types are compared, Python uses an
        arbitrary but consistent ordering. This can be very useful at times,
        for example when maintaining and searching a sorted list.

        If Python started trying to guess your intentions, and assumed that
        you really meant to compare the values, a whole load of quite basic
        things wouldn't work. Your example with different numbers may seem
        obvious, but what if the numbers were the same? Which of the following
        should return true?

        (50,) < 50
        (50,) == 50
        (50,) > 50

        If you think the middle one, what if the difference between having a
        tuple and a number is important?

        If you think that all should return false, you're not going to have
        much luck with a lot of standard algorithms - it would violate a basic
        assumption about ordering relations.

        Personally, I might have gone with 'it makes no sense to compare
        different types so raise an exception'. Possibly. But probably not.
        Pythons system works very well in practice.


        In Python, if you want to compare the actual numbers you need to
        compare the actual numbers. It works out better in the long run when
        your language isn't always second guessing what it thinks you really
        meant.


        --
        Steve Horne

        steve at ninereeds dot fsnet dot co dot uk

        Comment

        • Stephen Horne

          #5
          Re: I just don't get it

          On Sat, 28 Feb 2004 00:14:29 +0000, Stephen Horne
          <steve@ninereed s.fsnet.co.uk> wrote:
          [color=blue]
          >If Python started trying to guess your intentions, and assumed that
          >you really meant to compare the values, a whole load of quite basic
          >things wouldn't work.[/color]

          Just thought I'd chuck in a different example of what could happen if
          Python started second guessing intentions.

          (1,) == 1 .......... the numbers are both 1, after all

          (1,) == () ......... maybe you're just checking they're both
          tuples, after all?

          But then for both those to be true, you also get...

          1 == () ............ because 1 == (1,) == ()
          2 == () ............ because 2 == (2,) == () by the same logic

          1 == 2 ............. because 1 == () == 2


          In short, you either have to live with inconsistencies in ordering
          relationships that break standard algorithms and lead to all kinds of
          unexpected problems in any program over about 100 lines of code, or
          you have the ultimate egalitarian society of objects where every
          object is equal to every other object, irrespective of value or
          anything else.

          Neither seems attractive to me.


          --
          Steve Horne

          steve at ninereeds dot fsnet dot co dot uk

          Comment

          • Eduardo Elgueta

            #6
            Re: I just don't get it

            Stephen and everyone else,

            Thank you very much for your answers. I'm not new to Python, but I
            still have a long way to go

            Ed.

            Stephen Horne <steve@ninereed s.fsnet.co.uk> wrote in message news:<mcnv30h8h 6ltc9370gf7all5 mj58j4mjf0@4ax. com>...[color=blue]
            > On Sat, 28 Feb 2004 00:14:29 +0000, Stephen Horne
            > <steve@ninereed s.fsnet.co.uk> wrote:
            >[color=green]
            > >If Python started trying to guess your intentions, and assumed that
            > >you really meant to compare the values, a whole load of quite basic
            > >things wouldn't work.[/color]
            >
            > Just thought I'd chuck in a different example of what could happen if
            > Python started second guessing intentions.
            >
            > (1,) == 1 .......... the numbers are both 1, after all
            >
            > (1,) == () ......... maybe you're just checking they're both
            > tuples, after all?
            >
            > But then for both those to be true, you also get...
            >
            > 1 == () ............ because 1 == (1,) == ()
            > 2 == () ............ because 2 == (2,) == () by the same logic
            >
            > 1 == 2 ............. because 1 == () == 2
            >
            >
            > In short, you either have to live with inconsistencies in ordering
            > relationships that break standard algorithms and lead to all kinds of
            > unexpected problems in any program over about 100 lines of code, or
            > you have the ultimate egalitarian society of objects where every
            > object is equal to every other object, irrespective of value or
            > anything else.
            >
            > Neither seems attractive to me.[/color]

            Comment

            Working...