According to Python id
documentation returns the object's identity which in CPython is its memory address, and the is
operator checks if two objects are identical (which I assume should be in the same RAM memory). Consider the snippet below:
>>> a = "999"
>>> b = a
>>> id(tuple(a)) == id(tuple(b))
True
>>> tuple(b) is tuple(a)
False
Why the second expression evaluates to False
?
I read this question and this one but still don't know (though have some guesses) what's the exact cause for the behavior. Because both objects are still in memory and not simple strings or integers to be interned. I couldn't completely decipher the generated
byte-code using the dis.dis
.
I appreciate if you take the time to provide some explanation or reference before marking it as duplicate or down-voting.