gpt4 book ai didi

python - 用户定义类的默认哈希值是什么?

转载 作者:行者123 更新时间:2023-12-02 02:21:04 25 4
gpt4 key购买 nike

docs错误地声称

Objects which are instances of user-defined classes are hashable by default; they all compare unequal (except with themselves), and their hash value is their id()

虽然我记得这曾经是正确的,但在当前版本的 python(v2.7.10、v3.5.0)中,此类对象散列等于其 id 显然是不正确的。

>>> class A:
... pass
...
>>> a = A()
>>> hash(a)
-9223372036578022804
>>> id(a)
4428048072

another part文档中说哈希是从 id 派生而来的。实现何时/为何发生变化,以及现在哈希返回的数字是如何“派生自”id 的?

最佳答案

相关函数似乎是:

Py_hash_t
_Py_HashPointer(void *p)
{
Py_hash_t x;
size_t y = (size_t)p;
/* bottom 3 or 4 bits are likely to be 0; rotate y by 4 to avoid
excessive hash collisions for dicts and sets */
y = (y >> 4) | (y << (8 * SIZEOF_VOID_P - 4));
x = (Py_hash_t)y;
if (x == -1)
x = -2;
return x;
}

(该代码来自 here ,然后用作 type here 中的 tp_hash 槽。)那里的评论似乎给出了一个原因不直接使用指针(与id相同)。事实上,引入该函数更改的提交是 here ,并指出更改的原因是:

Issue #5186: Reduce hash collisions for objects with no hash method by rotating the object pointer by 4 bits to the right.

指的是this问题,这更多地解释了进行更改的原因。

关于python - 用户定义类的默认哈希值是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33590873/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com