Однако набор не удаляет один из двух идентичных экземпляров объекта. Что я должен определить, чтобы создать его? < /p>
Вот код Python. < /p>
Код: Выделить всё
class Item(object):
def __init__(self, foo, bar):
self.foo = foo
self.bar = bar
def __repr__(self):
return "Item(%s, %s)" % (self.foo, self.bar)
def __eq__(self, other):
if isinstance(other, Item):
return ((self.foo == other.foo) and (self.bar == other.bar))
else:
return False
def __ne__(self, other):
return (not self.__eq__(other))
< /code>
интерпретатор < /strong> < /p>
>>> set([Item(1,2), Item(1,2)])
set([Item(1, 2), Item(1, 2)])
Примечание: < /em>
Код: Выделить всё
Item
Код: Выделить всё
__hash__()
Подробнее здесь: https://stackoverflow.com/questions/394 ... -does-an-o