<p><strong>TL;DR</strong></p>
<p>将<code>frozen=True</code>与<code>eq=True</code>结合使用(这将使实例不可变)</p>
<p><strong>长答案</strong></p>
<p>从<a href="https://docs.python.org/3/library/dataclasses.html#module-level-decorators-classes-and-functions" rel="noreferrer">docs</a>开始:</p>
<blockquote>
<p><code>__hash__()</code> is used by built-in <code>hash()</code>, and when objects are added to hashed collections such as dictionaries and sets. Having a <code>__hash__()</code>
implies that instances of the class are immutable. Mutability is a
complicated property that depends on the programmer’s intent, the
existence and behavior of <code>__eq__()</code>, and the values of the eq and
frozen flags in the <code>dataclass()</code> decorator.</p>
<p>By default, <code>dataclass()</code> will not implicitly add a <code>__hash__()</code> method
unless it is safe to do so. Neither will it add or change an existing
explicitly defined <code>__hash__()</code> method. Setting the class attribute
<code>__hash__ = None</code> has a specific meaning to Python, as described in the <code>__hash__()</code>documentation.</p>
<p>If <code>__hash__()</code> is not explicit defined, or if it is set to None, then
<code>dataclass()</code> may add an implicit <code>__hash__()</code> method. Although not
recommended, you can force <code>dataclass()</code> to create a <code>__hash__()</code> method
with <code>unsafe_hash=True</code>. This might be the case if your class is
logically immutable but can nonetheless be mutated. This is a
specialized use case and should be considered carefully.</p>
<p>Here are the rules governing implicit creation of a <code>__hash__()</code> method.
Note that you cannot both have an explicit <code>__hash__()</code> method in your
dataclass and set <code>unsafe_hash=True</code>; this will result in a <code>TypeError</code>.</p>
<p>If eq and frozen are both true, by default <code>dataclass()</code> will generate a
<code>__hash__()</code> method for you. If eq is true and frozen is false, <code>__hash__()</code> will be set to None, marking it unhashable (which it is, since it is mutable). If eq is false, <code>__hash__()</code> will be left
untouched meaning the <code>__hash__()</code> method of the superclass will be used
(if the superclass is object, this means it will fall back to id-based
hashing).</p>
</blockquote>