擅长:python、mysql、java
<p>然而,当重载<a href="https://docs.python.org/3.4/reference/datamodel.html#object.__eq__" rel="noreferrer">^{<cd1>}</a>、<a href="https://docs.python.org/3.4/reference/datamodel.html#object.__ne__" rel="noreferrer">^{<cd2>}</a>和<a href="https://docs.python.org/3.4/reference/datamodel.html#object.__hash__" rel="noreferrer">^{<cd3>}</a>是有意义的时候,它可能是开箱即用的。默认情况下,<code>__eq__</code>将比较对象标识。这可能不是你想要的。在这种情况下,必须注意相等的对象具有相等的散列,并且,理想情况下,不相等的对象具有不同的散列(虽然这不是必需的,但它只是减少了冲突)。您应该始终使用<code>__eq__</code>实现<code>__ne__</code>,除非您有特殊的原因要这样做(这样做是为了确保逻辑一致性)。</p>
<p>此外,当重载<code>__hash__</code>时,必须注意当对象存储在<code>set</code>中时哈希不会更改。</p>