Locked learning resources

You must own this product to watch this lesson.

Locked learning resources

You must own this product to watch this lesson.

Small Integer Caching

This lesson is from the Real Python video course by Martin Breuss.

00:00 I’ll put it for you under the title of small integer caching. Okay? Keep that in mind. And let’s take a look at what’s happening here.

00:09 If I say a = 30 and b = 30, so same thing as before, but we’re using 30 instead of 300. What we would expect is a == b to be True, which it is.

00:24 And the id(a) == id(b), remember that this points to the location of the object in memory, so we would expect that to be False. However, in Python this is True.

00:37 And that’s kind of weird and could trip you up at the beginning if you don’t know about small integer caching, which is the solution of “Why is this happening?” And very simply, Python caches small integers. So, what’s a small integer? In Python, a small integer is anything between -5 and 256.

00:55 Everything in there gets cached. Let’s talk about what that means. I’ll roll it up again from the beginning with a couple of slides.

01:04 So, what we did is we assigned n and m both to the value 300, but they were both pointing to different objects, right?

01:12 So n was pointing to an integer object 300 and m was pointing to a new, different integer object with the same value, also 300.

01:23 So that’s why if we said n == m we would get True, same value, and id(n) did not equal id(m). You see? It’s two different objects.

01:34 Now, what happened when I said n = 30 and m = 30 as well—so, same type of assignment? Python actually is pointing us to the same object.

01:45 The reason being this cache, the small integer cache. At startup of an interpreter session, Python simply creates the objects for all of the small integers from -5 up to 256. It already creates these integer objects simply because these numbers are used so frequently that it’s better for performance to already have the objects there and just make it every time refer to the same object.

02:10 So this is why id(a) and id(b)—in this case, because it’s 30, which is smaller than 256—is actually pointing to the exact same object in memory. We can check on its number, in that case.

02:26 This is its location in memory. It’s going to be different when I start off a new IPython interpreter session because these integer objects get created on startup.

02:37 So, this is interesting and important to remember, because what I noted here is that essentially what happens is the same as if we would assign m to n. We saw this graphic before—n pointing to 30 and m pointing to 30—when we did this type of assignment.

02:54 And this is what happens with small integers even if we assign them on different lines and you would expect them to create different objects.

03:02 So, important to remember: any integer from -5 to 256 is going to be just assigned at startup, and every time you would refer to it in your program, it’s going to refer to an already existing object.

03:15 Anything above that, 257 upwards—it’s going to create new integer objects. So that’s a bit weird, but it’s a fun thing to know and it’s going to help you if you ever want to be in a Python pub quiz—which is something that happened to me at the PyCon! We got this question, exactly this question, and we had a couple of minutes to solve it.

03:37 So I want to give this to you as a challenge, because it’s a fun thing to think about. And there’s also a nice explanation that is going to be linked down here under the video. So check it out, but give it a try first and see if you can solve this.

03:49 The question is: what is a going to be? Without running the program, can you figure out what is a going to be? Just try to think through it, the different steps in this for loop, and keep in mind what happens with small integers in Python. I hope you’ll have fun. And don’t check the solution too quickly—just give it a go.

You must own this product to join the conversation.