πŸ’Ύ Archived View for dcreager.net β€Ί languages β€Ί python β€Ί hash-method-removed.gmi captured on 2024-12-17 at 09:33:22. Gemini links have been rewritten to link to archived content

View Raw

More Information

-=-=-=-=-=-=-

The β€˜__hash__’ method can be removed

If a class implements both β€˜__eq__’ and β€˜__hash__’, it's important for those implementations to be consistent with each other:

a == b ⇔ hash(a) == hash(b)

TIL that Python will try to help you adhere to this constraint in the presence of subclasses. If you define a subclass, and implement β€˜__eq__’ _but not_ β€˜__hash__’, then Python will implicitly set β€˜__hash__’ to β€˜None’ in that class. That way you can't accidentally override the meaning of β€œequals” in a way that breaks hashability.

β€˜object.__hash__’ documentation

This has some ramifications with the type system:

β€˜__hash__’, β€˜__eq__’, and LSP [Python Discourse]

Β» Languages Β» Python