<p>从<a href="https://docs.python.org/3/library/dataclasses.html#dataclasses.dataclass" rel="noreferrer">the docs</a>:</p>
<blockquote>
<p>Here are the rules governing implicit creation of a <code>__hash__()</code> method:</p>
<p>[...]</p>
<p>If <code>eq</code> and <code>frozen</code> are both true, by default <code>dataclass()</code> will
generate a <code>__hash__()</code> method for you. If <code>eq</code> is true and <code>frozen</code>
is false, <code>__hash__()</code> will be set to <code>None</code>, marking it unhashable
(which it is, since it is mutable). If <code>eq</code> 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>
<p>由于您设置了<code>eq=True</code>并将<code>frozen</code>保留为默认值(<code>False</code>),因此您的数据类是不可损坏的</p>
<p>您有3种选择:</p>
<ul>
<li>设置<code>frozen=True</code>(除了<code>eq=True</code>),这将使您的类不可变和可散列</李>
<li><p>Set<code>unsafe_hash=True</code>,这将创建一个<code>__hash__</code>方法,但使您的类保持可变状态,因此,如果在dict或Set中存储时修改了类的实例,则可能会出现问题:</p>
<pre><code>cat = Category('foo', 'bar')
categories = {cat}
cat.id = 'baz'
print(cat in categories) # False
</code></pre></li>
<li>手动实现<code>__hash__</code>方法</李>
</ul>