The Sunday tip #1: Python cached integers

1 minute read


Hey guys, today’s post is just a small tip that inaugurate a new series: the Sunday tips. In this series I will give you a brief Python tip so to whet your pythonic appetite before starting a new working week. Let me know if you like the idea in the comment below or maybe [buy me a coffee]

This week tip is about…

Python integer cache.

Let’s start with an example, open your REPL and try this:

>>> a = 666
>>> b = 666
>>> a is b
>>> False

As you can see we have assigned the value 666 to the variable a and then the value 666 to the variable b. Unsurprisingly, the two variables are pointing to two different objects.

So, what’s strange with that?

Well… nothing… unless the interpreter doesn’t behave differently depending on the number we assign… Let’s try with this second example:

>>> a = 3
>>> b = 3
>>> a is b
>>> True

What? Why the two variables a and b are now pointing to the same obejct?

No, you Python interpreter is not /religious/, it doesn’t consider the number 3 better or worse than the number 666, it just cache the values between -5 and 256 for performance reasons.

That’s cool uh?

Yes, but this is what I get with the REPL, would it be the same with the compiler? Well, apparently the compiler behave in a different way and analyzing your script, it may decide to cache also different numbers:

a = 3
b = 3
c = 666
d = 666
e = 5.5
f = 5.5
g = 11 / 2

print (a is b)
print (c is d)
print (e is f)
print (f is g)

And running this example you will get:


So, what have we learned?

  • your compiler does optimize more than you think
  • what you get in the REPL is not always identical to what you get from the compiler
  • even float numbers may be cached
  • not every number is cached ;)

Happy Sunday!


Did you find this article helpful?
Buy me a coffee!Buy me a coffee!