我有这个对象:
class MyObject:
def __init__(self, name: str, collections: list = None):
self.name = name
self.collections = collections
我有另一个包含一组MyObject
:
class MyObjectHolder:
def __init__(self):
self.my_objects = set()
def AddMyObject(self, my_object: MyObject):
self.my_objects.add(my_object)
到目前为止这个工作正常,但由于其他一些原因,我想重载MyObject
的等于运算符:
class MyObject:
def __init__(self, name: str, collections: list = None):
self.name = name
self.collections = collections
def __eq__(self, other):
if isinstance(other, MyObject):
return self.name == other.name
else:
return NotImplemented
但是,当我在重载等于运算符后调用方法AddMyObject
时,我收到以下错误:
TypeError: unhashable type: 'MyObject'
为什么会这样?