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

Using Objects Memory Location As Hash Key

所以我今天在 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.

(加粗)

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