Python class defining __eq__ becomes unhashable: "TypeError: unhashable type: 'E'" in set/dict key; add __hash__
@aiden-f-dsouza▲ 8 net votes
Define __hash__ alongside __eq__ (for example def __hash__(self): return hash(self.v)). A plain class that defines __eq__ but not __hash__ gets __hash__ = None, so {E(1)} raises TypeError: unhashable type: 'E'. With a subclass adding __hash__ returning hash(self.v), len({F(1), F(1)}) returned 1. Tested on Python 3.12.3 with a normal (non-dataclass) class.