<legend id='7WFS5'><style id='7WFS5'><dir id='7WFS5'><q id='7WFS5'></q></dir></style></legend>
        <bdo id='7WFS5'></bdo><ul id='7WFS5'></ul>

      <small id='7WFS5'></small><noframes id='7WFS5'>

      <i id='7WFS5'><tr id='7WFS5'><dt id='7WFS5'><q id='7WFS5'><span id='7WFS5'><b id='7WFS5'><form id='7WFS5'><ins id='7WFS5'></ins><ul id='7WFS5'></ul><sub id='7WFS5'></sub></form><legend id='7WFS5'></legend><bdo id='7WFS5'><pre id='7WFS5'><center id='7WFS5'></center></pre></bdo></b><th id='7WFS5'></th></span></q></dt></tr></i><div id='7WFS5'><tfoot id='7WFS5'></tfoot><dl id='7WFS5'><fieldset id='7WFS5'></fieldset></dl></div>
    1. <tfoot id='7WFS5'></tfoot>

      定义 `__eq__` 的类型是不可散列的?

      时间:2024-04-20
      • <legend id='5M9ro'><style id='5M9ro'><dir id='5M9ro'><q id='5M9ro'></q></dir></style></legend>

      • <small id='5M9ro'></small><noframes id='5M9ro'>

          <tbody id='5M9ro'></tbody>
        <i id='5M9ro'><tr id='5M9ro'><dt id='5M9ro'><q id='5M9ro'><span id='5M9ro'><b id='5M9ro'><form id='5M9ro'><ins id='5M9ro'></ins><ul id='5M9ro'></ul><sub id='5M9ro'></sub></form><legend id='5M9ro'></legend><bdo id='5M9ro'><pre id='5M9ro'><center id='5M9ro'></center></pre></bdo></b><th id='5M9ro'></th></span></q></dt></tr></i><div id='5M9ro'><tfoot id='5M9ro'></tfoot><dl id='5M9ro'><fieldset id='5M9ro'></fieldset></dl></div>

                <bdo id='5M9ro'></bdo><ul id='5M9ro'></ul>
                <tfoot id='5M9ro'></tfoot>
              • 本文介绍了定义 `__eq__` 的类型是不可散列的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                I had a strange bug when porting a feature to the Python 3.1 fork of my program. I narrowed it down to the following hypothesis:

                In contrast to Python 2.x, in Python 3.x if an object has an __eq__ method it is automatically unhashable.

                Is this true?

                Here's what happens in Python 3.1:

                >>> class O(object):
                ...     def __eq__(self, other):
                ...         return 'whatever'
                ...
                >>> o = O()
                >>> d = {o: 0}
                Traceback (most recent call last):
                  File "<pyshell#16>", line 1, in <module>
                    d = {o: 0}
                TypeError: unhashable type: 'O'
                

                The follow-up question is, how do I solve my personal problem? I have an object ChangeTracker which stores a WeakKeyDictionary that points to several objects, giving for each the value of their pickle dump at a certain time point in the past. Whenever an existing object is checked in, the change tracker says whether its new pickle is identical to its old one, therefore saying whether the object has changed in the meantime. Problem is, now I can't even check if the given object is in the library, because it makes it raise an exception about the object being unhashable. (Cause it has a __eq__ method.) How can I work around this?

                解决方案

                Yes, if you define __eq__, the default __hash__ (namely, hashing the address of the object in memory) goes away. This is important because hashing needs to be consistent with equality: equal objects need to hash the same.

                The solution is simple: just define __hash__ along with defining __eq__.

                这篇关于定义 `__eq__` 的类型是不可散列的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:证书验证失败:无法获取本地颁发者证书 下一篇:except-clause 删除局部变量

                相关文章

                1. <i id='0etFM'><tr id='0etFM'><dt id='0etFM'><q id='0etFM'><span id='0etFM'><b id='0etFM'><form id='0etFM'><ins id='0etFM'></ins><ul id='0etFM'></ul><sub id='0etFM'></sub></form><legend id='0etFM'></legend><bdo id='0etFM'><pre id='0etFM'><center id='0etFM'></center></pre></bdo></b><th id='0etFM'></th></span></q></dt></tr></i><div id='0etFM'><tfoot id='0etFM'></tfoot><dl id='0etFM'><fieldset id='0etFM'></fieldset></dl></div>
                  <legend id='0etFM'><style id='0etFM'><dir id='0etFM'><q id='0etFM'></q></dir></style></legend>
                2. <small id='0etFM'></small><noframes id='0etFM'>

                3. <tfoot id='0etFM'></tfoot>
                    <bdo id='0etFM'></bdo><ul id='0etFM'></ul>