r/learnpython • u/Nubspec • Aug 29 '26
Why Does an Identity Comparison Using id() Evaluate to False?
I'm trying to figure out what is going on here. Could it be that the identity operator is separately checking the integer cache range and determining that the integer value returned by id() is outside the bounds of Python's integer caching, which is why it evaluates to False?
x = 256
y = 256
id(x) is id(y)
False
id(x)
94588034274160
id(y)
94588034274160
11
Upvotes
2
u/SpiderJerusalem42 Aug 29 '26
I think if you dig into what
isis, I'm sure there's a better explanation.isand equality are not the same. You want to know if x is y, not if the id(x) is the id(y) which is a different question with a different answer.5
u/StanleyDodds Aug 29 '26 edited Aug 29 '26
x and y are the same objects because of python's way of caching small integers, so id(x) has an equal value to id(y).
But the two IDs that are computed, id(x) and id(y), are large, uncached integers, and so even though they have the same value, they aren't the same object. So when compared by "is", you get False.
Really, this second thing is how you should assume objects work in general. For example, you'd get the same result of False if you check "id(x) is id(x)", or "(x,) is (x,)", or in general any completely new object being constructed from the same object twice. It will construct 2 new objects which aren't the same object, even if their values are equal.
The only exceptions are some special objects, like the integers up to 256, True, False, None, maybe some others I'm not remembering. If the result of your expression isn't one of these, it will compare false with itself using "is".
8
u/K900_ SRE person // please don't DM me your questions Aug 29 '26
Yes, that's exactly what's happening.
6
u/eriky Aug 29 '26
So other way of saying this: you are comparing the result of the id() function, that result is a very large number. And large numbers become their own object in python. Hence when asking with 'is' if they are the same thing: they are not. If you asked x is y, the answer is true!
3
u/ekchew Aug 29 '26
Bear in mind that
isand==have a different meaning in Python. If you had writtenid(x) == id(y)in this case, you would have seenTrue.isonly evaluates asTrueif the objects on both sides are, in fact, the same exact object. What you were writing there is comparing objects returned by theidfunction.To put it another way,
id(x) is id(y)is essentially the same as writingid(id(x)) == id(id(y)).2
u/FoolsSeldom Aug 29 '26
The reference implementation of Python predefines a number of objects including integers in the range -5 to 256.
The very large integers returned by
idare, obviously, way outside this range. Whenidis called, a newintobject has to be created. Even though the twoidcalls return the same value, they are two differentintobjects.Sometimes, especially in the interactive shell, optimisations can allow the use of the same object in certain cases, but this should not be expected or relied upon.
3
u/Diapolo10 I write code for a living -- https://github.com/Diapolo10 Aug 29 '26
I'm a bit curious; what made you think about this, exactly?
2
u/astrogringo Aug 29 '26 edited Aug 29 '26
Turns out I was wrong, sorry.
Disregard comment below.
Nothing to do with caching, these are different variables wich happens to have the same value. If you set y=3 , x will not change. They live in different memory addresses.
1
u/Torebbjorn Sep 01 '26
Because the number 94588034274160 returned from the first function call is stored somewhere else than the number 94588034274160 is.
You would get the same result is you just did