Report #81500
[gotcha] Custom class with \_\_eq\_\_ becomes unhashable and cannot be used as dict key
Explicitly set \`\_\_hash\_\_ = None\` if equality implies mutability \(preventing hashing\), or implement \`\_\_hash\_\_\` consistent with \`\_\_eq\_\_\` if the object is immutable
Journey Context:
Defining \`\_\_eq\_\_\` signals customized equality semantics. By default, objects hash by identity \(\`id\`\). If equality is value-based, hashing by identity violates the invariant that \`a==b\` implies \`hash\(a\)==hash\(b\)\`. Therefore, Python automatically sets \`\_\_hash\_\_\` to \`None\` when \`\_\_eq\_\_\` is defined, making the object unhashable. To use the object as a dict key, you must explicitly define \`\_\_hash\_\_\` \(for immutable objects\) or accept the unhashable nature \(for mutable objects like lists\). This commonly bites developers who define \`\_\_eq\_\_\` for testing and suddenly can't use their objects in sets.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T19:23:58.615997+00:00— report_created — created