0
x1 = 51
x2 = 51
id(x1)
id(x2)
id(x1) is id(x2)

As I know, Python creates an object and references to it. So id(x1) and id(x2) are the same. But why does the line id(x1) is id(x2) result in False?

kaya3
  • 47,440
  • 4
  • 68
  • 97
  • 4
    Because the `id` function doesn't guarantee to return the same integer object on repeated calls, just the same integer value. Try `id(x1) == id(x2)`. – kaya3 Feb 28 '20 at 03:14
  • 2
    Note, what *should* be surprising is that `id(x1)` and `id(x2)` are the same. – juanpa.arrivillaga Feb 28 '20 at 03:16
  • I'm not sure whether this is really a duplicate of [this question](https://stackoverflow.com/questions/306313/is-operator-behaves-unexpectedly-with-integers), but it's certainly related. The reason `x1` and `x2` have the same id but `id(x1)` and `id(x2)` don't is because 51 is small enough to be cached, whereas the id of the object 51 is a much larger number so it's not cached. – kaya3 Feb 28 '20 at 03:21
  • @juanpa.arrivillaga The id of both x1 and x2 is 4461071152 on my computer. – wong ricardo Feb 28 '20 at 03:26
  • Print `id(id(x1))` and `id(id(x2))`, and you will see that they are indeed two distinct objects (despite being integers with the same numeric value). – jasonharper Feb 28 '20 at 03:30
  • @wongricardo yes exactly *and that should be the surprising result* – juanpa.arrivillaga Feb 28 '20 at 03:57
  • @kaya3 That is really an Answer and you should post it as one, so that wong ricardo can flag it as accepted and the rest of us can upvote it. Because it **is** the answer. – Bernd Wechner Feb 28 '20 at 04:24
  • @BerndWechner Yes, I just wasn't sure whether it should be closed as a duplicate instead of answered; but I did post an answer (a bit before your comment). – kaya3 Feb 28 '20 at 11:47

2 Answers2

1

This question revolves around issues covered in these other questions:

The first thing you need to know is that the same value can be represented by multiple different objects. For example, you can have the number 5000 which is an int object, and another copy of the number 5000 as a different object int object at a different location in memory.

The second thing you need to know is that is tests if two objects are the same object at the same location in memory, not just that their values are equal (that's what == tests).

The third thing you need to know is that in current versions of CPython, the objects for small numbers like 51 are cached, so you don't get multiple copies of them; you always get the same object from the cache, even if 51 appears more than once in your program (except in obscure cases).

Given all of that, the behaviour you observe follows:

  • 51 is a small integer so there's only one object representing it.
  • x1 and x2 both hold references to that object, so id(x1) and id(x2) are the same memory address.
  • That memory address is a rather large number - 4461071152 when you tested it - so if it appears more than once in the program then you will usually get different copies of it at different memory locations.
  • So id(x1) is id(x2) is false because you called the id function twice, and it returned two different copies of the integer 4461071152, so they aren't the same object at the same memory location (which is what is tests).

Amusingly enough, you can also do id(x1) is id(x1) and the result is still False, for exactly the same reason. The practical conclusion is that you should use == to compare integers, not is.

kaya3
  • 47,440
  • 4
  • 68
  • 97
  • 1
    Upvoted, but should probably note that the small integer cache is an implementation detail, not a feature of the language itself. And even in CPython, there are some edge cases that might not utilize the cache – juanpa.arrivillaga Feb 28 '20 at 04:08
0

The "is" operator in python checks if they point to the same object where as the "==" operator will check if the values are same.

l1 = [1,2]
l2 = [1,2]

print (l1 == l2) #return true
print (l1 is l2) #returns false

When you do id(x1) is id(x2) its 2 temporary objects with different identity.

PraveenB
  • 1,270
  • 10
  • 11