class x:
def __init__(self,name):
self.name=name
def __str__(self):
return self.name
def __cmp__(self,other):
print("cmp method called with self="+str(self)+",other="+str(other))
return self.name==other.name
instance1=x("hello")
instance2=x("there")
print(instance1==instance2)
print(instance1.name==instance2.name)
The output is here:
cmp method called with self=hello,other=there
True
False
This is not what I expected: I'm trying to say that "two instances are equal if the name fields are equal."
If I'm just return Falseout of function __cmp__, this also shows up as True!! If I come back -1, then I get False- but since I'm trying to compare strings, this doesn't seem right.
What am I doing wrong here?
source
share