Identity: Recap
Here’s a recap of what you’ve covered about object identity:
- Object value vs object identity: Objects that have the same value can be separate objects.
- Small integer caching: The ID of two objects that you might expect to be different can be the same.
Check out the talk Turning ‘wat’ into ‘why’ by Katie McLaughlin that covers an explanation about small integer caching and specifically addresses the Pub Quiz challenge mentioned in this course.
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.
Anthony De La Rosa on Jan. 5, 2021
I’m a beginner and I’m currently doing the Learning Path: Intro to Python so the quiz is just not fair. We haven’t learned about ranges, for loops or even if statements so although I like a challenge, it would be nice if this was explained before given a quiz like that.
Martin Breuss RP Team on Jan. 6, 2021
Hi @Anthony, are you following the Learning Path Introduction to Python?
Did you maybe mix up the quizzes somehow? I just clicked through the Python Variables Quiz that follows this course, and it doesn’t include any questions about ranges, loops, or conditionals.
Where did you find the link to the quiz? Let us know so we can double-check that there’s no rogue link anywhere that points to a more advanced quiz too early! Thanks! :)
Anthony De La Rosa on Jan. 6, 2021
Hey Martin :)
I am following this link: realpython.com/learning-paths/python3-introduction/ I am basically following that list of courses one after the other. Sorry about the comment but I was thrown off by it. I plan to complete the whole list and move on to the next part.
Martin Breuss RP Team on Jan. 7, 2021
Hi again @Anthony, nice job that you’re going through the whole list! There’s a lot to learn but if you stick with it and keep going, things will start to come together :)
You should see the Learning Path like in this screenshot, which puts the Python Variables Quiz right after this course on Variables in Python.
Click that link and you’ll be in the right quiz that tests the topics covered in this course. Hope that helps and keep learning :)
Ashutosh Kumar on April 1, 2021
I tried print(id(a))
and print(id(b))
in sublime text editor using values as high as 3000. I am getting the same id for both “a” and “b”. Can someone please help me clarify?
Bartosz Zaczyński RP Team on April 2, 2021
@Ashutosh Kumar That’s interesting. Can you paste the exact piece of code that you’re running, please?
g2sgautam on April 17, 2021
@Ashutosh Kumar
I am using Python 3.7 and interning varies from version to version
Depends on how you execute the line and what python interpreter sees at the moment
In a python file
x = 1000056475364665758
y = 1000056475364665758
print(f"x address {id(x)}, y address {id(y)}")
OUTPUT x address 2111466067968, y address 2111466067968
In a REPL interpreter
>>> x = 1000056475364665758
>>> y = 1000056475364665758
>>> id(x), id(y)
(2319921208016, 2319921208056)
Also now if you change the way you execute
>>> x = 1000056475364665758; y = 1000056475364665758; id(x), id(y)
(2319922547288, 2319922547288)
When both the x and y values are visible to interpreter at once it can perform many optimizations on top of existing small integers cache.
For integers in range of -5 to 256 no matter how you execute it is always fetched from cache
>>> id(x), id(y)
(140706469252640, 140706469252640)
>>> x = 10; y=10; id(x), id(y)
(140706469244960, 140706469244960)
Martin Breuss RP Team on April 18, 2021
Great answer, thanks @g2sgautam! 🙌
lemaychris on May 5, 2021
Hello, I don’t see the links that discuss the pub quiz. Are they in this lecture or located somewhere else?
Thanks!
Chris
Martin Breuss RP Team on May 6, 2021
Hi @lemaychris, thanks for the heads-up! I’ve added the link in the Description tab of this video. Here it is again:
Biggz78 on Dec. 4, 2021
Cool quiz, I go it wrong thou as i was set on the wrong foot of by the “throwaway” variable
_ is not defined so it would print the global scope assignment of a..... wrong !!! lol
Ross on April 13, 2024
I’m surprised that after all the intro courses I’ve taken over the years, this is the first mention of small integer caching.
Become a Member to join the conversation.
Greish on Dec. 3, 2020
Thank you! Came for learning more about variables, did not get disappointed.