Been playing around with is and equality checks in Python. None is None returns True. My understanding is that there's only one None object at any point in the heap so every reference to None points to the same address. [] is [] returns False since [] is mutable and there can exist more than one objects in the heap.() is () also returns True. My explanation for this is that tuples are immutable so there's only one at any point in the heap. What I don't understand is why (1,) is (1,) returns False. Appreciate any response/corrections of my understanding.
Asked
Active
Viewed 87 times
2
-
"is" tests if its just a variable refers to the same object "identity" ie. x = (1,2,3); y = x; x is y will yield True. look here for more info https://stackoverflow.com/a/13650309/1400926 – EzzatA Feb 26 '18 at 15:21
-
On reflection the dupe may not answer your question exactly, but it is certainly relevant – Chris_Rands Feb 26 '18 at 15:23
-
@EzzatA He understand that; his question is why two empty tuples have the same identity, where two empty lists or dictionaries do not. – John Gordon Feb 26 '18 at 15:23
-
@JohnGordon Aha.. ok sorry missed that.. on side note when checking id([]) every time it yields a different id.. while an empty tuple will yield the same id every time. so the is behaviour is explained.. i do not know the reason why however.. – EzzatA Feb 26 '18 at 15:27
-
Thank you for the responses. as noted by John Gordon, my question is different to the one that has been marked as answered. I am interested in knowing why (1,) is (1,) is False. – paperino9 Feb 26 '18 at 15:51
-
1@Thomas Your question is in spirit the exact same. If you read the answer on the linked duplicate, it should be clear why your check is False. – miradulo Feb 26 '18 at 15:54
-
@miradulo You 're right, thank you and apologies for the duplicate. – paperino9 Feb 26 '18 at 15:59