a == A()
a == b
实际上是调用__eq__操作符重载来监测对象是否相等,可见
class A(object):
def __init__(self):
pass
def __eq__(self, other):
print "__eq__ called"
return True
a = A()
b = A()
print (a == A())
print(a == b)
监测对象地址用id()函数,比如 id(a) == id(b) 打印a和b是否指向一个地址, 比较对象内容和对象地址是2个意思,见
class A(object):
def __init__(self, _id, _name, _age):
self.mID = _id
self.mName = _name
self.mAge = _age
def __eq__(self, other):
return self.mID == other.mID
a = A(123, "tom", 23)
b = A(123, "jerry", 23)
c = A(234, "jerry", 23)
print a == b
print a == c
print id(a) == id(b)
附加,更新后的代码有几个问题
1) 判断语句应该是if type(self) == type(another) and self.__dict__ == another.__dict__,后面不是self,笔误
2) 你可以在__eq__里加上print "__eq__ called",明显是转换成set之后比较不是用这个__eq__,而是__hash__方法
3) 尽量不要用__dict__吧,个人觉得你显式用args已经很好了
4) 我个人不太喜欢用type来检查类型,更喜欢下面的方式
def __eq__(self, another):
assert isinstance(another, self.__class__)
return self.args == another.args