How to override the hash function of python data classes?

I am trying to write a base class for python dataclasse with a custom hash function as follows. However, when calling the child class’s hash it does not use the custom hash function of the parent class.

import dataclasses
import joblib

class HashableDataclass:

    def __hash__(self):
        print("Base class hash was called!")
        fields = dataclasses.fields(self)
        values = tuple(getattr(self, for field in fields)
        return int(joblib.hash(values), 16)

class MyDataClass1(HashableDataclass):
    field1: int
    field2: str

obj1 = MyDataClass1(1, "Hello")

Is there a way to override hash function of data classes?

>Solution :

You should check the documentation:

If eq and frozen are both true, by default dataclass() will generate a hash() method for you. If eq is true and frozen is false, hash() will be set to None, marking it unhashable (which it is, since it is mutable). If eq is false, hash() will be left untouched meaning the hash() method of the superclass will be used (if the superclass is object, this means it will fall back to id-based hashing).

@dataclasses.dataclass(frozen=True, eq=False)  # <- HERE
class MyDataClass1(HashableDataclass):
    field1: int
    field2: str


>>> obj1 = MyDataClass1(1, "Hello")
Base class hash was called!

Leave a Reply