python的id()函数返回指定对象的内存id值。每个对象拥有唯一的内存id。
在StackOverflow上有一个关于id()的十分有意思的问题:
http://stackoverflow.com/questions/40820787/is-there-a-difference-between-and-list-when-using-id
简单来说,有人提出了如下问题:
>>> [] is []
False
>>> id([]) == id([])
True
前一表达式与后一表达式结果不一致。
根本原因在于后者重用了丢弃对象的内存,使得两个[]的内存id一致。在python官方文档关于id()有介绍不同对象id()返回值一致的情况:
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.
具体在原帖中有详细的解释。