Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

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.

Avatar image for Greish

Greish on Dec. 3, 2020

Thank you! Came for learning more about variables, did not get disappointed.

Avatar image for Anthony De La Rosa

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.

Avatar image for Martin Breuss

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! :)

Avatar image for Anthony De La Rosa

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.

Avatar image for Martin Breuss

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 :)

Avatar image for Ashutosh Kumar

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?

Avatar image for Bartosz Zaczyński

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?

Avatar image for g2sgautam

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)
Avatar image for Martin Breuss

Martin Breuss RP Team on April 18, 2021

Great answer, thanks @g2sgautam! 🙌

Avatar image for lemaychris

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

Avatar image for Martin Breuss

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:

Turning ‘wat’ into ‘why’ by Katie McLaughlin

Avatar image for Biggz78

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

Avatar image for Ross

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.