Strange behaviour of __cmp__

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • janeaustine50@hotmail.com

    Strange behaviour of __cmp__

    See the following...
    [color=blue][color=green][color=darkred]
    >>> class X(list):[/color][/color][/color]
    def __cmp__(self,an X):
    print "comparing from",id(self)
    return cmp(self.v,anX. v)

    [color=blue][color=green][color=darkred]
    >>> x1=X()
    >>> x2=X()
    >>> x1.v=-1
    >>> x2.v=100
    >>> x1>x2[/color][/color][/color]
    False[color=blue][color=green][color=darkred]
    >>> x1<x2[/color][/color][/color]
    False

    I expected x1>x2 or x1<x2 were False and True respectively.

  • Robert Kern

    #2
    Re: Strange behaviour of __cmp__

    janeaustine50@h otmail.com wrote:[color=blue]
    > See the following...
    >
    >[color=green][color=darkred]
    >>>>class X(list):[/color][/color]
    >
    > def __cmp__(self,an X):
    > print "comparing from",id(self)
    > return cmp(self.v,anX. v)
    >
    >
    >[color=green][color=darkred]
    >>>>x1=X()
    >>>>x2=X()
    >>>>x1.v=-1
    >>>>x2.v=100
    >>>>x1>x2[/color][/color]
    >
    > False
    >[color=green][color=darkred]
    >>>>x1<x2[/color][/color]
    >
    > False
    >
    > I expected x1>x2 or x1<x2 were False and True respectively.[/color]

    list.__lt__ and list.__gt__ are being used in preference to X.__cmp__.
    Override these methods.

    --
    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

    Working...