473,320 Members | 1,820 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Problem comparing object graphs and trees


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 '06 #1
3 1064
ra************@gmail.com 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 '06 #2

I am not drunk but should have rtfm.

Sorry and Thanks.

r.

On Dec 15, 3:04 pm, Peter Otten <__pete...@web.dewrote:
raphael.mar...@gmail.com 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 '06 #3
ra************@gmail.com 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__.

Dec 15 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Ivan Pavlov | last post by:
Hello, I hope someone can help me with this. I have a large list of objects (think of them as car parts). Each part may be a substitute for some other part(s). E.g. p1 is a substitue for p2 and...
0
by: Tom | last post by:
Access97 I have an Excel worksheet that contains seven graphs. In my application I have seven subreports in an unbound report. In each subreport I Paste-Linked one of the graphs in the Excel...
19
by: Ole Nielsby | last post by:
How does the GetHashCode() of an array object behave? Does it combine the GetHashCode() of its elements, or does it create a sync block for the object? I want to use readonly arrays as...
2
by: Ron M. Newman | last post by:
Hi, I have a simple class that has a Hashtable. the hashtable has a couple of key/value pairs where the key is a string and the value is also a strong. I have at the top of that class. ...
1
by: dasarisrikar | last post by:
Hi, I am a basic learner in c and datastrustures..... I want to know the conepts of trees, graphs... could u provide me a proper tutorial or a good link which discusses trees and graphs concepts...
2
by: nallen05 | last post by:
A friend and I developed a method of querying graphs of data stored in a database with tree-regular expressions (using an XML syntax based on RELAX NG). We are trying to start a startup to...
3
by: rosun82 | last post by:
Recently I am trying to develop a C++ program dealing with large scale DAGs (about 6,000,000 nodes and 50,000,000 edges), and I find a very serious problem here. In the program, the node and edges...
3
momotaro
by: momotaro | last post by:
Could some one explain to me what are the criteria to choose array implementation or linked lists implementation or trees implementation or graphs implementation…? THK!
0
by: vmantrana | last post by:
Hi guys: I have been studying about serialization and I have found this concept: "object graphs", which is not very clear for me. I have found that it is a limitation of the XML serialization...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.