Singletons and interning in Python

Daniel Tooke
3 min readAug 6, 2019

--

Following on from my last post about objects being stored in memory addresses, one thing that blew my mind a little when I first discovered it is that not only are True, False, and None objects just like any other in Python, but they are singleton objects. That is to say that they exist only once and at one point in memory for the entire existence of your program. Every time your function returns True, it’s returning the very same object that pops up every other time you see True. You can’t initialise a second, different True object anywhere else in your program. The same for False and None. In other words:

True is True
False is False
None is None

This makes them singleton objects, existing once and only once, at one place in memory.

There are other singletons in memory too. When Python initialises, it will go through a process called interning. If a value is interned, Python will instantiate one and only one object of its kind, so that it always looks to the same object at the same memory address whenever that object is referenced. Among other things, Python will instantiate and intern the range of numbers -5 to 256 inclusive¹ all of which will be singleton objects. But numbers outside of this range will be instantiated as new integer objects as needed, and will be separate objects with different memory addresses:

>>> a = 256
>>> b = 256
>>> a == b
True
>>> a is b
True
>>> a = 257
>>> b = 257
>>> a == b
True
>>> a is b
False

As it happens, this came up in the solution to a Python brainteaser I saw the other day:

def check(x):
if x+1 is 1+x:
return False
if x+2 is not 2+x:
return False
return True

What could you possibly input as x to make this function return True? Well, because of the way Python interns a range of numbers on startup, the number -7 will return True.

Why? Because -6 is not interned (so line 2 is False, as the two -6 instances are not the same object) but -5 is interned (so on line 4, -5 is a singleton and the is not expression evaluates to False). As such, it returns True. Any other number returns False!

In short, singleton objects exist only once, and any variables that point to them will all point to the same object at the same memory address. As well as booleans and the None object, Python will intern the range of numbers -5 to 256 inclusive. It will also intern string objects under some circumstances, which will be the subject of my next post!

¹ This automatic interning actually happens as an extension of Python having been written in C. If you’re using anything other than standard CPython (such as JPython or IronPython) then a) your results might vary, and b) you probably know what you’re doing anyway.

This is the second of a series of articles I wrote on memory addresses in Python; here are the others:

I. Variables and memory addresses in Python

II. Singletons and interning in Python (this article)

III. Will Python intern my string?

IV. Delete delete delete! How Python collects your garbage for you

--

--