Every once in a while I face this dilemma and don't know what to do. Recently I have been working on an program where I am using sqlalchemy and making the class frozen=True
don't work well with it. The solution is to make unsafe_hash=True
(which obviously makes a beginner like me frightened in some situations). I fell like I should always use eq=True
instead of unsafe_hash=True
, but on the other hand, having a set would remove a lot of loops and conditional logic from my code.
For a more practical example, this post has an implementation that makes me in doubt if it is a good solution, since it require the need to enforce that name
and dob
never change (Python dataclass generate hash and exclude unsafe fields). In my case I would do that to use a person
in a set()
. Also, I might want to change the value of name
for some random reason.
What is the ideal solution? Does it make any sense to have some kind of randomly generated attribute for such a class (using uuid maybe) and use it in the __hash__
? And how would I enforce that the hash would never change?