08-24-2020, 03:36 PM
Python Small Integer Caching: == versus is
This interesting code snippet was brought to my attention by Finxter reader Albrecht.
You’d guess that the for loop goes from to , each time incrementing and . As Python creates one integer object to which both names refer, the command should always be . Thus, the result is , right?
WRONG!!! $%&&%$
Try it yourself in our interactive code shell:
https://trinket.io/embed/python/c5b84ba145
Exercise: Run the code and check the result. Did you expect this?
The result is.
The reason is an implementation detail of the CPython implementation called “Small Integer Caching” — the internal cache of integers in Python.
![[Image: objects-scaled.jpg?fit=1024%2C576&ssl=1]](https://i0.wp.com/blog.finxter.com/wp-content/uploads/2019/12/objects-scaled.jpg?fit=1024%2C576&ssl=1)
If you create an integer object that falls into the range of -5 to 256, Python will only return a reference to this object — which is already cached in memory.
You can visualize the code execution in this interactive memory visualizer:
https://pythontutor.com/iframe-embed.htm...nces=false
Exercise: Click next until you see the result. How many integers are in memory?
Let’s quickly examine the meaning of “is” in Python.
The is operator
The is operator checks if two variable names point to the same object in memory:
Both variables and point to the string . Python doesn’t store the same string twice but creates it only once in memory. This saves memory and makes Python faster and more efficient. And it’s not a problem because strings are immutable — so one variable cannot “overshadow” a string object of another variable.
Note that we can use the function to check an integer representation of the memory address:
They both point to the same location in memory! Therefore, the operator returns !
Small Integer Caching
Again, if you create an integer object that falls into the range of -5 to 256, Python will only return a reference to this object — which is already cached in memory. But if we create an integer object that does not fall into this range, Python may return a new integer object with the same value.
![[Image: objects2-scaled.jpg?fit=1024%2C576&ssl=1]](https://i1.wp.com/blog.finxter.com/wp-content/uploads/2019/12/objects2-scaled.jpg?fit=1024%2C576&ssl=1)
If we now check, Python will give us the correct result .
In fact, this leads to the strange behavior of the C implementation of Python 3:
Therefore, you should always compare integers by using the operator in Python. This ensures that Python performs a semantic comparison, and not a mere memory address comparison:
What can you learn from this? Implementation details matter!
Where to Go From Here?
Enough theory, let’s get some practice!
To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?
Practice projects is how you sharpen your saw in coding!
Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?
Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.
Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.
Join the free webinar now!
This interesting code snippet was brought to my attention by Finxter reader Albrecht.
Code:
a, b = 250, 250
for i in range(250, 260): if a is not b: break a += 1 b += 1
print(a)
# What's the output of this code snippet?You’d guess that the for loop goes from
Code:
i=250Code:
i=259Code:
aCode:
bCode:
a is not bCode:
FalseCode:
a=259WRONG!!! $%&&%$
Try it yourself in our interactive code shell:
https://trinket.io/embed/python/c5b84ba145
Exercise: Run the code and check the result. Did you expect this?
The result is
Code:
a=257The reason is an implementation detail of the CPython implementation called “Small Integer Caching” — the internal cache of integers in Python.
![[Image: objects-scaled.jpg?fit=1024%2C576&ssl=1]](https://i0.wp.com/blog.finxter.com/wp-content/uploads/2019/12/objects-scaled.jpg?fit=1024%2C576&ssl=1)
If you create an integer object that falls into the range of -5 to 256, Python will only return a reference to this object — which is already cached in memory.
Quote:“The current implementation keeps an array of integer objects for all integers between -5 and 256, when you create an int in that range you actually just get back a reference to the existing object.”
Python Docs
You can visualize the code execution in this interactive memory visualizer:
https://pythontutor.com/iframe-embed.htm...nces=false
Exercise: Click next until you see the result. How many integers are in memory?
Let’s quickly examine the meaning of “is” in Python.
The is operator
The is operator checks if two variable names point to the same object in memory:
Code:
>>> a = "hello"
>>> b = "hello"
>>> a is b
TrueBoth variables
Code:
aCode:
bCode:
"hello"Note that we can use the
Code:
id()Code:
>>> a = "hello"
>>> b = "hello"
>>> id(a)
1505840752992
>>> id(b)
1505840752992They both point to the same location in memory! Therefore, the
Code:
isCode:
TrueSmall Integer Caching
Again, if you create an integer object that falls into the range of -5 to 256, Python will only return a reference to this object — which is already cached in memory. But if we create an integer object that does not fall into this range, Python may return a new integer object with the same value.
![[Image: objects2-scaled.jpg?fit=1024%2C576&ssl=1]](https://i1.wp.com/blog.finxter.com/wp-content/uploads/2019/12/objects2-scaled.jpg?fit=1024%2C576&ssl=1)
If we now check
Code:
a is not bCode:
TrueIn fact, this leads to the strange behavior of the C implementation of Python 3:
Code:
>>> a = 256
>>> b = 256
>>> a is b
True
>>> a = 257
>>> b = 257
>>> a is b
FalseTherefore, you should always compare integers by using the
Code:
==Code:
>>> a = 256
>>> b = 256
>>> a == b
True
>>> a = 257
>>> b = 257
>>> a == b
TrueWhat can you learn from this? Implementation details matter!
Where to Go From Here?
Enough theory, let’s get some practice!
To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?
Practice projects is how you sharpen your saw in coding!
Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?
Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.
Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.
Join the free webinar now!

