I already looked at this question, but I don't really get why the two id()
calls return an identical value, while the is
comparison returns False
.
>>> [2,2,2] + [1] == [2,2,2,1]
True
>>> [2,2,2] + [1] is [2,2,2,1]
False
>>> id([2,2,2] + [1])
4396847688
>>> id([2,2,2,1])
4396847688
To be sure, I did expect the two comparisons to return True
and False
as they did, I just don't get why the ids are not different.
Just re-entering my comment as "an answer":
Simply because the same memory is reused for the objects: in the last two statements, the lifetimes of the objects whose id's you're obtaining do not overlap, so Python is free to (re)use the same memory for them.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments