Problem comparing object graphs and trees

Discussion in 'Python' started by raphael.marvie@gmail.com, Dec 15, 2006.

  1. Guest

    Dear all,

    I am trying to compare graphes of object through the use of the __cmp__
    operator. Before managing the problem of recursive comparison, I have
    tried a simple test which result surprises me. Here is the simplest
    code I can write that presents my problem:

    </pre>
    $ cat cmp.py

    class A:
    def __init__(self, b):
    self.b = b
    def __cmp__(self, other):
    return self.b == other.b

    class B:
    pass

    if __name__ == '__main__':
    b = B()
    a1 = A(b)
    a2 = A(b)
    print a1 == a2
    print a1 == a1

    $ python cmp.py
    False
    False
    </pre>

    I swear I am not drunk, but why isn't a1 == a2 and worse why isn't a1
    == a1? Does someone have a clue and can explain to me this suprising
    behavior? (python 2.4.3 on Ubuntu 6.06).

    Cheers,

    r.
    , Dec 15, 2006
    #1
    1. Advertising

  2. Peter Otten Guest

    wrote:

    > </pre>
    > $ cat cmp.py
    >
    > class A:
    > def __init__(self, b):
    > self.b = b
    > def __cmp__(self, other):
    > return self.b == other.b
    >
    > class B:
    > pass
    >
    > if __name__ == '__main__':
    > b = B()
    > a1 = A(b)
    > a2 = A(b)
    > print a1 == a2
    > print a1 == a1
    >
    > $ python cmp.py
    > False
    > False
    > </pre>
    >
    > I swear I am not drunk, but why isn't a1 == a2 and worse why isn't a1
    > == a1? Does someone have a clue and can explain to me this suprising
    > behavior? (python 2.4.3 on Ubuntu 6.06).


    __cmp__() must return 0 for equal objects:

    >>> 1 .__cmp__(0), 1 .__cmp__(1), 1 .__cmp__(2)

    (1, 0, -1)

    You might have a look at rich comparison before you proceed:

    >>> class A(object):

    .... def __init__(self, b):
    .... self.b = b
    .... def __eq__(self, other):
    .... return self.b == other.b
    ....
    >>> A(1) == A(1)

    True
    >>> A(1) == A(42)

    False

    Peter
    Peter Otten, Dec 15, 2006
    #2
    1. Advertising

  3. Guest

    I am not drunk but should have rtfm.

    Sorry and Thanks.

    r.

    On Dec 15, 3:04 pm, Peter Otten <> wrote:
    > wrote:
    > > </pre>
    > > $ cat cmp.py

    >
    > > class A:
    > > def __init__(self, b):
    > > self.b = b
    > > def __cmp__(self, other):
    > > return self.b == other.b

    >
    > > class B:
    > > pass

    >
    > > if __name__ == '__main__':
    > > b = B()
    > > a1 = A(b)
    > > a2 = A(b)
    > > print a1 == a2
    > > print a1 == a1

    >
    > > $ python cmp.py
    > > False
    > > False
    > > </pre>

    >
    > > I swear I am not drunk, but why isn't a1 == a2 and worse why isn't a1
    > > == a1? Does someone have a clue and can explain to me this suprising
    > > behavior? (python 2.4.3 on Ubuntu 6.06).__cmp__() must return 0 for equal objects:

    >
    > >>> 1 .__cmp__(0), 1 .__cmp__(1), 1 .__cmp__(2)(1, 0, -1)

    >
    > You might have a look at rich comparison before you proceed:
    >
    > >>> class A(object):... def __init__(self, b):

    > ... self.b = b
    > ... def __eq__(self, other):
    > ... return self.b == other.b
    > ...>>> A(1) == A(1)
    > True
    > >>> A(1) == A(42)False

    >
    > Peter
    , Dec 15, 2006
    #3
  4. Duncan Booth Guest

    wrote:

    > I swear I am not drunk, but why isn't a1 == a2 and worse why isn't a1
    >== a1? Does someone have a clue and can explain to me this suprising
    > behavior? (python 2.4.3 on Ubuntu 6.06).


    Because the __cmp__ method doesn't return a boolean. It returns a value <0,
    0 or >0 according to the relative ordering of its arguments. You are
    returning True (1) when the values are equal and you should be returning 0.

    If ordering isn't defined for your objects then define __eq__ instead of
    __cmp__.
    Duncan Booth, Dec 15, 2006
    #4
    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. =?Utf-8?B?RGF2aWQgSmVzc2Vl?=

    ObjectDataSource - Object Graphs

    =?Utf-8?B?RGF2aWQgSmVzc2Vl?=, Dec 15, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    1,184
    =?Utf-8?B?RGF2aWQgSmVzc2Vl?=
    Dec 16, 2005
  2. Jean-Marie Condom

    JAXB : comparing and updating trees

    Jean-Marie Condom, Dec 28, 2004, in forum: Java
    Replies:
    0
    Views:
    367
    Jean-Marie Condom
    Dec 28, 2004
  3. Alia Khouri
    Replies:
    4
    Views:
    274
    Alia K
    Mar 31, 2009
  4. Franken Sense

    comparing binary trees in C

    Franken Sense, May 1, 2009, in forum: C Programming
    Replies:
    12
    Views:
    663
    Franken Sense
    May 9, 2009
  5. jacob navia

    Binary search trees (AVL trees)

    jacob navia, Jan 3, 2010, in forum: C Programming
    Replies:
    34
    Views:
    1,417
    Dann Corbit
    Jan 8, 2010
Loading...

Share This Page