This lesson is from the Real Python video course by Martin Breuss.
Identity: Recap
00:00
Here’s a quick recap on this object identity section. At first, you looked at object value versus object identity. You saw that even though we assign n
to 300
and m
to 300
, we create two different objects. That’s what normally happens.
00:16
And we can see that they have the same value, which we can check with this operator ==
. n == m
equals True
. But the identity is different, because they’re two different objects.
00:28
So while id(n)
is some certain number, it’s a different number than the id(m)
simply because they’re two different objects in memory. That’s the standard and that’s how it usually is, and then we took a look at small integer caching. And, surprisingly, it’s different there.
00:47
The ID of two objects that we would expect to be different are actually the same. And the reason for this is small integer caching in Python. Keep this list in mind—it’s simply all the numbers from -5
up to 256
. They get assigned on startup of your interpreter, the objects get created, and if you then refer to any of those numbers, they always refer to the same object. And that’s why.
01:10 And then we took a look at a little pub quiz question, and I’ll put this up again for you at the end now so that you can take a peek and try to solve it. If you haven’t yet, give it a go, and check out the links for a nice and thorough explanation of why is this going on. I’ll just keep it up here and see you in the next video.
You must own this product to join the conversation.