gpt4 book ai didi

python - 使用对象内存位置作为哈希键

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

所以我今天意识到在 Python 中我们可以使用对象内存地址位置作为字典中的键。

当位于该内存位置的对象被另一个对象替换时,python 如何防止冲突?

使用内存位置作为散列键还有其他风险吗?

最佳答案

我认为您是在谈论一个对象的id,而不是它本身的内存地址。它们在 CPython 中是相同的,但那是一个实现细节。

如果是这样,那么文档会回答您的问题:

id(object)

Return the "identity" of an object. This is an integer which is guaranteed to be unique and constant for this object during its lifetime. Two objects with non-overlapping lifetimes may have the same id() value.

CPython implementation detail: This is the address of the object in memory.

(加粗)

换句话说,它不会防止碰撞。

关于python - 使用对象内存位置作为哈希键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67256977/

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