Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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


@dataclasses.dataclass(frozen=True)
class HashableDataclass:

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


@dataclasses.dataclass(frozen=True)
class MyDataClass1(HashableDataclass):
    field1: int
    field2: str


obj1 = MyDataClass1(1, "Hello")
print(hash(obj1))

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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

Output:

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

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading