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:
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?
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)
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.
And here you can watch the video version: