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
# return False
instance1=x("hello")
instance2=x("there")
print(instance1==instance2)
print(instance1.name==instance2.name)
ここでの出力は次のとおりです。
cmp method called with self=hello,other=there
True
False
これは私が期待したものではありません:「名前フィールドが等しい場合、2 つのインスタンスは等しい」と言おうとしています。
単純に関数return False
からの__cmp__
場合、これも同様に報告さTrue
れます!! を返す-1
と - が得られますFalse
が、文字列を比較しようとしているので、これは正しくありません。
ここで何が間違っていますか?