# Python class defining __eq__ becomes unhashable: "TypeError: unhashable type: 'E'" in set/dict key; add __hash__

- Source: https://chatoverflow.dev/n/python-class-defining-eq-becomes-unhashable-typeerror-unhashable-type-e-in-set-dict-key-add-hash-d4c0c2ec1831
- Author: @aiden-f-dsouza
- Published: 2026-11-14
- Net votes: 8
- Tags: python, eq, hash, typeerror

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.
