Inconsistency in Python's Comparisons

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dietrich Epp

    Inconsistency in Python's Comparisons

    Without invoking double-underscore black magic, it is possible to
    choose a, b, and c in Python such that:

    a < b
    b < c
    c < a

    This could cause sorting functions to malfunction.
    [color=blue][color=green][color=darkred]
    >>> class t(object):[/color][/color][/color]
    .... pass
    ....[color=blue][color=green][color=darkred]
    >>> a = t()
    >>> b = u'0'
    >>> c = '1'
    >>> a < b[/color][/color][/color]
    True[color=blue][color=green][color=darkred]
    >>> b < c[/color][/color][/color]
    True[color=blue][color=green][color=darkred]
    >>> c < a[/color][/color][/color]
    True

    Possible kludge fix: Change default_3way_co mpare() to use "basestring "
    as the typename for both built-in string types.

    Possible elegant fix: Any class whose members may either be less than
    or greater than members of a different class, which often share a
    common superclass, use the name of the superclass for comparisons with
    objects of incompatible types. This parallels the comparison of
    numeric types because they compare as if their type name were empty.


  • Erik Max Francis

    #2
    Re: Inconsistency in Python's Comparisons

    Dietrich Epp wrote:
    [color=blue]
    > This could cause sorting functions to malfunction.
    >[color=green][color=darkred]
    > >>> class t(object):[/color][/color]
    > ... pass
    > ...[color=green][color=darkred]
    > >>> a = t()
    > >>> b = u'0'
    > >>> c = '1'
    > >>> a < b[/color][/color]
    > True[color=green][color=darkred]
    > >>> b < c[/color][/color]
    > True[color=green][color=darkred]
    > >>> c < a[/color][/color]
    > True[/color]

    Do you have an example of this that doesn't involve comparisons between
    different types? In Python, except for complex numbers, comparisons
    between instances of different types is guaranteed to be consistent, but
    not necessarily helpful.

    If you're doing comparisons, not to mention sorting, between instances
    of fundamentally incommensurable types (like instances of a class and
    strings), I'd say that's programmer error, although admittedly it is a
    little strange that you found a circular situation like that.

    --
    __ Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
    / \ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
    \__/ Liberty without learning is always in peril; learning without
    liberty is always in vain. -- John F. Kennedy

    Comment

    • Dietrich Epp

      #3
      Re: Inconsistency in Python's Comparisons

      [replying to both replies]

      On Mar 16, 2004, at 6:06 PM, Erik Max Francis wrote:
      [color=blue]
      > Do you have an example of this that doesn't involve comparisons between
      > different types? In Python, except for complex numbers, comparisons
      > between instances of different types is guaranteed to be consistent,
      > but
      > not necessarily helpful.
      >
      > If you're doing comparisons, not to mention sorting, between instances
      > of fundamentally incommensurable types (like instances of a class and
      > strings), I'd say that's programmer error, although admittedly it is a
      > little strange that you found a circular situation like that.[/color]

      I guess I could write a patch... I suppose my complaint is that rich
      comparison with different types neither raises an error nor gives
      consistent results, and I expect either one or the other. I'd be
      scared to write a patch that added a field to the type structure, and
      I'd be a bit ashamed to write a kludge patch.

      I wouldn't say comparing different different types is necessarily an
      error, though. The last time I sorted a list, it was heterogeneous --
      it contained different file classes, some with paths, and some
      temporary files which couldn't have paths by design (both subclasses of
      object). I sorted it so the output wouldn't be in some arbitrary,
      impossible-to-read order, the program didn't depend on the order at
      all.

      I wonder if this could benefit performance? I.e, when comparing
      objects of different types, if the 'comparison class' (number,
      basestring, etc.) is compared first, other comparisons might be
      skipped. This is an edge case though, so nobody should notice/nobody
      will care/any detriment to more common cases should be avoided.

      I've got a lot of other patches for other software I could be writing,
      so I suppose this isn't really important at all.



      Comment

      • Dan Bishop

        #4
        Re: Inconsistency in Python's Comparisons

        Erik Max Francis <max@alcyone.co m> wrote in message news:<4057B2A4. F9EC49DD@alcyon e.com>...[color=blue]
        > Dietrich Epp wrote:
        >[color=green]
        > > This could cause sorting functions to malfunction.
        > > [example of nontransitive "<"][/color]
        >
        > Do you have an example of this that doesn't involve comparisons between
        > different types?[/color]

        # This shouldn't count, for obvious reasons. But what the heck...

        class ContrivedExampl e(object):
        def __init__(self, n):
        self.__n = n
        def __cmp__(self, other):
        if self.__n == other.__n:
        return 0
        elif (self.__n - other.__n) % 3 == 1:
        return -1
        else:
        return 1

        paper = ContrivedExampl e(0)
        rock = ContrivedExampl e(1)
        scissors = ContrivedExampl e(2)

        print paper < scissors
        print scissors < rock
        print rock < paper

        Comment

        • Erik Max Francis

          #5
          Re: Inconsistency in Python's Comparisons

          Dan Bishop wrote:
          [color=blue]
          > # This shouldn't count, for obvious reasons. But what the heck...[/color]

          He explicitly restricted himself to not invoking "double-underscore
          magic." You can certainly make classes which don't constitute a
          well-ordering, and you can certainly get bizarre behavior by relying on
          the (arbitrary) ordering between types, but the only thing I'd be
          worried about is such a circular ordering between types in the same
          class.
          [color=blue]
          > paper = ContrivedExampl e(0)
          > rock = ContrivedExampl e(1)
          > scissors = ContrivedExampl e(2)[/color]

          I do like your example, however :-).

          --
          __ Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
          / \ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
          \__/ You and I / We've seen it all / Chasing our hearts' desire
          -- The Russian and Florence, _Chess_

          Comment

          • Erik Max Francis

            #6
            Re: Inconsistency in Python's Comparisons

            Dietrich Epp wrote:
            [color=blue]
            > I wouldn't say comparing different different types is necessarily an
            > error, though.[/color]

            It's guaranteed to be consistent, and sort is guaranteed not to go nuts
            (as someone else pointed out). The ordering is defined to be
            consistent, although it does not define a well-ordering, and furthermore
            need not be the same between different implementations (or even
            different implementations ). It may not be an error, but there's no
            reason for it to be a total ordering.

            --
            __ Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
            / \ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
            \__/ You and I / We've seen it all / Chasing our hearts' desire
            -- The Russian and Florence, _Chess_

            Comment

            • Andrew Koenig

              #7
              Re: Inconsistency in Python's Comparisons

              "Erik Max Francis" <max@alcyone.co m> wrote in message
              news:4057B2A4.F 9EC49DD@alcyone .com...
              [color=blue]
              > Do you have an example of this that doesn't involve comparisons between
              > different types?[/color]

              Do long and float count as different types?


              Comment

              • Peter Hansen

                #8
                Re: Inconsistency in Python's Comparisons

                Andrew Koenig wrote:
                [color=blue]
                > "Erik Max Francis" <max@alcyone.co m> wrote in message
                > news:4057B2A4.F 9EC49DD@alcyone .com...
                >
                >[color=green]
                >>Do you have an example of this that doesn't involve comparisons between
                >>different types?[/color]
                >
                >
                > Do long and float count as different types?[/color]

                Was this a trick question?
                [color=blue][color=green][color=darkred]
                >>> float[/color][/color][/color]
                <type 'float'>[color=blue][color=green][color=darkred]
                >>> long[/color][/color][/color]
                <type 'long'>[color=blue][color=green][color=darkred]
                >>> issubclass(long , long)[/color][/color][/color]
                True[color=blue][color=green][color=darkred]
                >>> issubclass(long , float)[/color][/color][/color]
                False[color=blue][color=green][color=darkred]
                >>> issubclass(floa t, long)[/color][/color][/color]
                False

                Python says the answer to your question is "yes"...

                -Peter

                Comment

                • Andrew Koenig

                  #9
                  Re: Inconsistency in Python's Comparisons

                  "Peter Hansen" <peter@engcorp. com> wrote in message
                  news:Ze6dncncZq 9qCcXdRVn-tA@powergate.ca ...[color=blue]
                  > Andrew Koenig wrote:[/color]
                  [color=blue][color=green]
                  > > "Erik Max Francis" <max@alcyone.co m> wrote in message
                  > > news:4057B2A4.F 9EC49DD@alcyone .com...[/color][/color]
                  [color=blue][color=green][color=darkred]
                  > >>Do you have an example of this that doesn't involve comparisons between
                  > >>different types?[/color][/color][/color]
                  [color=blue][color=green]
                  > > Do long and float count as different types?[/color]
                  >
                  > Was this a trick question?[/color]

                  Not particularly. Although long and float are obviously different types, I
                  suspect that most people who say that they do not expect comparisons between
                  different types to work, they do expect comparisons to work consistently
                  between integers and floating-point values.


                  Comment

                  • Dietrich Epp

                    #10
                    Re: Inconsistency in Python's Comparisons

                    [replying to both replies]

                    On Mar 16, 2004, at 6:06 PM, Erik Max Francis wrote:
                    [color=blue]
                    > Do you have an example of this that doesn't involve comparisons between
                    > different types? In Python, except for complex numbers, comparisons
                    > between instances of different types is guaranteed to be consistent,
                    > but
                    > not necessarily helpful.
                    >
                    > If you're doing comparisons, not to mention sorting, between instances
                    > of fundamentally incommensurable types (like instances of a class and
                    > strings), I'd say that's programmer error, although admittedly it is a
                    > little strange that you found a circular situation like that.[/color]

                    I guess I could write a patch... I suppose my complaint is that rich
                    comparison with different types neither raises an error nor gives
                    consistent results, and I expect either one or the other. I'd be
                    scared to write a patch that added a field to the type structure, and
                    I'd be a bit ashamed to write a kludge patch.

                    I wouldn't say comparing different different types is necessarily an
                    error, though. The last time I sorted a list, it was heterogeneous --
                    it contained different file classes, some with paths, and some
                    temporary files which couldn't have paths by design (both subclasses of
                    object). I sorted it so the output wouldn't be in some arbitrary,
                    impossible-to-read order, the program didn't depend on the order at
                    all.

                    I wonder if this could benefit performance? I.e, when comparing
                    objects of different types, if the 'comparison class' (number,
                    basestring, etc.) is compared first, other comparisons might be
                    skipped. This is an edge case though, so nobody should notice/nobody
                    will care/any detriment to more common cases should be avoided.

                    I've got a lot of other patches for other software I could be writing,
                    so I suppose this isn't really important at all.



                    Comment

                    • Josiah Carlson

                      #11
                      Re: Inconsistency in Python's Comparisons

                      > Not particularly. Although long and float are obviously different types, I[color=blue]
                      > suspect that most people who say that they do not expect comparisons between
                      > different types to work, they do expect comparisons to work consistently
                      > between integers and floating-point values.[/color]

                      That is only because floats, integers and longs all represent numbers.
                      It makes sense to most people to compare numbers.

                      - Josiah

                      Comment

                      • Erik Max Francis

                        #12
                        Re: Inconsistency in Python's Comparisons

                        Dietrich Epp wrote:
                        [color=blue]
                        > I guess I could write a patch... I suppose my complaint is that rich
                        > comparison with different types neither raises an error nor gives
                        > consistent results, and I expect either one or the other. I'd be
                        > scared to write a patch that added a field to the type structure, and
                        > I'd be a bit ashamed to write a kludge patch.[/color]

                        The question here is what you think the goal of writing a patch would
                        be. You're trying to modify behavior that is as-designed.

                        --
                        __ Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
                        / \ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
                        \__/ Come not between the dragon and his wrath.
                        -- King Lear (Act I, Scene I)

                        Comment

                        Working...