2

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.

damian
  • 165
  • 3
  • 4
    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. – Tim Peters Jun 05 '17 at 00:15
  • That makes sense, thanks! If it were an answer, I'd accept it! – damian Jun 05 '17 at 00:16

1 Answers1

4

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.

Tim Peters
  • 67,464
  • 13
  • 126
  • 132