Skip to content
Home » Cache Memory with Interning in Python

Cache Memory with Interning in Python

Spread the love

Today we’ll see how Python uses interning to help you cache memory.

Object Identities

First, let’s create two variables and check their identities:

>>> a = 5
>>> b = 5
>>> id(a), id(b)
(140728848340128, 140728848340128)

So, turns out a and b are referencing the same object:

cache memory with interning

How about this:

>>> a = -3
>>> b = -3
>>> id(a), id(b)
(140728848339872, 140728848339872)

Again, the identities are the same, so a and b are referencing the same object.

So, how about strings?

>>> a = "help"
>>> b = "help"
>>> id(a), id(b)
(2640671603544, 2640671603544)

Seems to work, too. And now let’s try this:

>>> a = 300
>>> b = 300
>>> id(a), id(b)
(2640705563472, 2640705563376)

Surprise! Although we assigned the same value to a and b, just like before, this time the id function gives us different results, which means we have two different objects here, although they both have the same value: 300.

Python Will Cache Memory with Interning

So, what’s going on? In the first couple of examples we had the same results from the id function, and now we have different results.

Before I explain this, let’s try one more example. This time with a string:

>>> a = "help!"
>>> b = "help!"
>>> id(a), id(b)
(2640705534248, 2640705534024)

Again, we have two separate objects, both with the same value of “help!”.

So, why do we sometimes have the same results from the id function and sometimes not? Or, in other words, why do our a and b variables sometimes reference the same object and on other occasions two individual objects with the same value?

Your Panda3D Magazine

Make Awesome Games and Other 3D Apps

with Panda3D and Blender using Python.

Cool stuff, easy to follow articles.

Get the magazine here (PDF).

Well, this is because of interning, which is the topic of this article. When interning works, multiple variables can reference the same object in memory. Let’s have a look at the following example:

>>> a = 200
>>> b = 200
>>> c = 200
>>> id(a), id(b), id(c), id(200)
(140728848346368, 140728848346368, 140728848346368, 140728848346368)

When interning doesn’t work, individual objects with different ids are created:

>>> a = 400
>>> b = 400
>>> c = 400
>>> id(a), id(b), id(c), id(400)
(2640705561200, 2640705563120, 2640705562160, 2640705561072)

Python Jumpstart Course

Learn the basics of Python, including OOP.

with lots of exercises, easy to follow

The course is available on Udemy.

When Does Interning Work?

Interning is used to save memory. It works for integers ranging between -5 and 255 and for strings without whitespaces and special characters. In other cases, if we create another variable with the same value, a new object is created and assigned to the new variable.

Now you know why it worked for the integer 200, but not for 400. It’s because 400 is beyond the range between -5 and 255. You also know why it worked for “help”, but not for “help!”. In the latter case there’s a special character, so there’s no interning. It doesn’t work with whitespaces, either, hence the following results:

>>> a = "setup"
>>> b = "setup"
>>> id(a), id(b)
(2640705533800, 2640705533800)
>>> 
>>> a = "set up"
>>> b = "set up"
>>> id(a), id(b)
(2640705534248, 2640705533912)

So, this is how interning works. It’s used by Python to cache memory.

Blender Jumpstart Course

Learn the basics of 3D modeling in Blender.

step-by-step, easy to follow, visually rich

The course is available on Udemy and on Skillshare.

And here you can watch the video version:


Spread the love

Leave a Reply