1

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?

Joaquim
  • 111
  • 1
  • 10
  • Welcome to [Stack Overflow.](https://stackoverflow.com/ "Stack Overflow")! Questions that ask for general guidance regarding a problem approach are typically too broad and are not a good fit for this site. People have their own method for approaching the problem and because of this there cannot be a correct answer. Give a good read over [Where to Start](https://softwareengineering.meta.stackexchange.com/questions/6366/where-to-start/6367#6367), and [Minimal Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example "Minimal Reproducible Example") then edit your post. – itprorh66 Nov 17 '21 at 15:59
  • 1
    The concept of an "unsafe" hash should always frighten, beginners and those familiar with Python alike :-) – rv.kvetch Nov 17 '21 at 16:15

0 Answers0