Python interpreter bug

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Robert Kern

    #16
    Re: Python interpreter bug

    alainpoint@yaho o.fr wrote:[color=blue]
    > In fact, i want to sort the list based on the 'allocated attribute' and
    > at the same time, test membership based on the id attribute.
    > __cmp__ logically implies an ordering test, not an identity test. These
    > two notions seems to be confounded in python which is unfortunate. Two
    > objects could have the same rank while still being different.[/color]

    In that case, define __lt__ and __eq__ separately instead of __cmp__.
    list.sort() will use __lt__ if it's provided rather than __cmp__.

    --
    Robert Kern
    rkern@ucsd.edu

    "In the fields of hell where the grass grows high
    Are the graves of dreams allowed to die."
    -- Richard Harter

    Comment

    • bruno modulix

      #17
      Re: Python interpreter bug

      alainpoint@yaho o.fr wrote:[color=blue]
      > Sorry Fredrik but I don't understand. Just comment out the assert and
      > you have different results depending on whether an unrelated sort
      > function is defined.
      > This seems weird to me ![/color]

      code snippet:
      [color=blue]
      > from random import choice
      > class OBJ:
      > def __init__(self,i dentifier):
      > self.id=identif ier
      > self.allocated= 0 # Your problem begins here...[/color]
      [color=blue]
      > def __cmp__(self,ot her):[/color]
      # it continues here[color=blue]
      > return cmp(other.alloc ated,self.alloc ated)
      > mylist=[OBJ(i) for i in range(20)]
      > excluded=[obj for obj in mylist if obj.id>choice(r ange(20))]
      > for obj in mylist:
      > if obj in excluded: # and you see it in action here
      > assert obj.id in [objt.id for objt in excluded]
      > continue[/color]

      How do you think the "membership " test works ? Could it be possible that
      it uses the __cmp__ method ? And if so, how do you think your instances
      will compare knowing that your __cmp__ method will return equality for
      all the instances in this snippet ?

      Just change your __init__ method to:

      def __init__(self,i dentifier):
      self.id=identif ier
      self.allocated= identifier

      and rerun the test.

      I don't think this is a bug...

      --
      bruno desthuilliers
      python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
      p in 'onurb@xiludom. gro'.split('@')])"

      Comment

      • Fredrik Lundh

        #18
        Re: Python interpreter bug

        alainpoint@yaho o.fr wrote:[color=blue]
        > In fact, i want to sort the list based on the 'allocated attribute' and
        > at the same time, test membership based on the id attribute.
        > __cmp__ logically implies an ordering test[/color]

        really?



        com·pare
        v. com·pared, com·par·ing, com·pares
        v. tr.
        1. To consider or describe as similar, equal, or analogous; liken.
        2. To examine in order to note the similarities or differences of.
        /.../
        [color=blue]
        > not an identity test.[/color]

        it's not an identity test. it's a comparision, and the "in" operator
        uses it to determine *equality*, not identity. you need to read
        up on object comparisions in the reference manual.

        </F>



        Comment

        • alainpoint@yahoo.fr

          #19
          Re: Python interpreter bug

          No doubt you're right but common sense dictates that membership testing
          would test identity not equality.
          This is one of the rare occasions where Python defeats my common sense
          ;-(

          Alain

          Comment

          • Christopher Subich

            #20
            Re: Python interpreter bug

            alainpoint@yaho o.fr wrote:[color=blue]
            > No doubt you're right but common sense dictates that membership testing
            > would test identity not equality.
            > This is one of the rare occasions where Python defeats my common sense[/color]

            But object identity is almost always a fairly ill-defined concept.
            Consider this (Python 2.2, 'cuz that's what I have right now):

            Python 2.2.3 (#1, Nov 12 2004, 13:02:04)
            [GCC 3.2.3 20030502 (Red Hat Linux 3.2.3-42)] on linux2
            Type "help", "copyright" , "credits" or "license" for more information.[color=blue][color=green][color=darkred]
            >>> al = "ab"
            >>> alp = al+"c"
            >>> alphabet = "abc"
            >>> al[/color][/color][/color]
            'ab'[color=blue][color=green][color=darkred]
            >>> alp[/color][/color][/color]
            'abc'[color=blue][color=green][color=darkred]
            >>> alphabet[/color][/color][/color]
            'abc'[color=blue][color=green][color=darkred]
            >>> alp is alphabet[/color][/color][/color]
            0[color=blue][color=green][color=darkred]
            >>> alp == alphabet[/color][/color][/color]
            1 # True on Py2.4


            The only reliable thing that object identity tells you is "these two
            foos occupy the same memory location." Even for identical, immutable
            objects, this may not be true (see case above) -- some immutables do end
            up occupying the same memory location (try i=1;j=2;k=j-1;i is k), but
            this is by no means guraranteed, and indeed only happens sometimes
            because of optimizations.

            Comment

            • Fredrik Lundh

              #21
              Re: Python interpreter bug

              alainpoint@yaho o.fr wrote:
              [color=blue]
              > No doubt you're right but common sense dictates that membership testing
              > would test identity not equality.[/color]

              what does "common sense" have to say about this case:
              [color=blue][color=green][color=darkred]
              >>> L = ("aa", "bb", "cc", "dd")
              >>> S = "a" + "a"
              >>> L[/color][/color][/color]
              ('aa', 'bb', 'cc', 'dd')[color=blue][color=green][color=darkred]
              >>> S[/color][/color][/color]
              'aa'[color=blue][color=green][color=darkred]
              >>> S in L[/color][/color][/color]
              # True or False ?

              </F>



              Comment

              • Steve Holden

                #22
                Re: Python interpreter bug

                alainpoint@yaho o.fr wrote:[color=blue]
                > I understand this, Steve.
                > I thought the _cmp_ method was a helper for sorting purposes. Why is it
                > that a membership test needs to call the __cmp__ method?[/color]

                Can you suggest another way to test for set membership, given that
                instances aren't singletons? The only way to do so is to iterate over
                the list, asking "are these the same instance", followed (in the case of
                a "no" answer) by "are these two instances equal?".

                Consider:
                [color=blue][color=green][color=darkred]
                >>> a = {1:'one'}
                >>> b = {2:'two'}
                >>> c = {1:'one'}
                >>> a is c[/color][/color][/color]
                False[color=blue][color=green][color=darkred]
                >>> a in [b, c][/color][/color][/color]
                True[color=blue][color=green][color=darkred]
                >>>[/color][/color][/color]

                What would you have Python do differently in these circumstances?
                [color=blue]
                > If this isn't a bug, it is at least unexpected in my eyes.[/color]

                Ah, right. So it's your eyes that need fixing! :-)
                [color=blue]
                > Maybe a candidate for inclusion in the FAQ?
                > Thank you for answering[/color]

                A pleasure.

                regards
                Steve
                --
                Steve Holden +44 150 684 7255 +1 800 494 3119
                Holden Web LLC www.holdenweb.com
                PyCon TX 2006 www.python.org/pycon/

                Comment

                • alainpoint@yahoo.fr

                  #23
                  Re: Python interpreter bug

                  > Steve Holden wrote:[color=blue]
                  >Consider:[/color]
                  [color=blue][color=green][color=darkred]
                  >>> a = {1:'one'}
                  >>> b = {2:'two'}
                  >>> c = {1:'one'}
                  >>> a is c[/color][/color][/color]
                  False[color=blue][color=green][color=darkred]
                  >>> a in [b, c][/color][/color][/color]
                  True[color=blue][color=green][color=darkred]
                  >>>[/color][/color][/color]

                  [color=blue]
                  >What would you have Python do differently in these circumstances?[/color]

                  You mean: What i would do i if i was the benevolent dictator ?
                  I would make a distinction between mutables and immutables. Immutables
                  would test for equality and mutables would test for identity.
                  Membership testing for objects is a very common use case which is
                  totally unrelated to their being sorted according to a key.
                  I am no expert on languages so i could be wrong. Don't hesitate to
                  correct me.
                  Alain

                  Comment

                  • Steve Holden

                    #24
                    Re: Python interpreter bug

                    alainpoint@yaho o.fr wrote:[color=blue][color=green]
                    >>Steve Holden wrote:
                    >>Consider:[/color]
                    >
                    >[color=green][color=darkred]
                    > >>> a = {1:'one'}
                    > >>> b = {2:'two'}
                    > >>> c = {1:'one'}
                    > >>> a is c[/color][/color]
                    > False[color=green][color=darkred]
                    > >>> a in [b, c][/color][/color]
                    > True[color=green][color=darkred]
                    > >>>[/color][/color]
                    >
                    >[color=green]
                    >>What would you have Python do differently in these circumstances?[/color]
                    >
                    >
                    > You mean: What i would do i if i was the benevolent dictator ?
                    > I would make a distinction between mutables and immutables. Immutables
                    > would test for equality and mutables would test for identity.[/color]

                    Which is exactly the wrong way round.
                    [color=blue]
                    > Membership testing for objects is a very common use case which is
                    > totally unrelated to their being sorted according to a key.
                    > I am no expert on languages so i could be wrong. Don't hesitate to
                    > correct me.
                    > Alain
                    >[/color]
                    It's not worth bothering - just work with Python how it is, and enjoy
                    the language!

                    regards
                    Steve
                    --
                    Steve Holden +44 150 684 7255 +1 800 494 3119
                    Holden Web LLC www.holdenweb.com
                    PyCon TX 2006 www.python.org/pycon/

                    Comment

                    Working...