Skip to main content
 首页 » 编程设计

python 列表 id(L) 和 id(L[ :])

2025年05月04日16zhengyun_ustc

很好奇id(L)和id(L[:])的区别和关系,其中L是一个列表。

官方文档对 id() 是这样说的:

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

但是我不明白为什么id(L)和id(L[:])有不同的值。

请您参考如下方法:

L[:] 生成列表的副本。它是一个全新的列表,包含与 L 相同的项目。因此,它存储在内存中的不同位置并具有不同的 ID。