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. 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
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
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__. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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.
...
|
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...
|
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...
|
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...
|
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!
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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)...
|
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....
|
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
|
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...
| |