DEV Community

Dolly Sharma
Dolly Sharma

Posted on

🧠 What Python Caches — and What It Does Not

Understanding Python’s Object Caching, Interning, and Memory Behavior

Python performs several smart memory optimizations behind the scenes. One of the most important is object caching (interning).

Understanding this helps you:

  • avoid tricky is bugs
  • write memory-aware code
  • answer tricky interview questions confidently

🔷 What Is Object Caching in Python?

Python uses object caching (also called interning) as a performance optimization.

Instead of creating new objects every time, Python reuses certain immutable objects to:

  • reduce memory usage
  • improve execution speed

⚠️ Important: Caching is an implementation detail of CPython, not a strict language guarantee.


✅ Objects Python Does Cache

1️⃣ Small Integers

a = 10
b = 10
print(a is b)   # True
Enter fullscreen mode Exit fullscreen mode

📌 Cached range in CPython: -5 to 256

What happens:

  • Same integer object reused
  • Safe because integers are immutable

2️⃣ Boolean Values

a = True
b = True
print(a is b)   # True
Enter fullscreen mode Exit fullscreen mode

Facts:

  • Only two boolean objects exist
  • True and False are singletons
  • Always cached

3️⃣ None

a = None
b = None
print(a is b)   # True
Enter fullscreen mode Exit fullscreen mode

None is a singleton — there is only one instance in the entire program.

✅ Always safe to use is None.


4️⃣ Interned Strings (Conditional)

a = "hello"
b = "hello"
print(a is b)   # Often True
Enter fullscreen mode Exit fullscreen mode

Python may intern:

  • short strings
  • identifiers
  • strings with letters, numbers, underscores

⚠️ Not guaranteed

a = "hello world"
b = "hello world"
print(a is b)   # True or False (implementation-dependent)
Enter fullscreen mode Exit fullscreen mode

❌ Objects Python Does Not Cache

1️⃣ Large Integers

a = 1000
b = 1000
print(a is b)   # Usually False
Enter fullscreen mode Exit fullscreen mode

👉 New object typically created.


2️⃣ Floating-Point Numbers

a = 10.5
b = 10.5
print(a is b)   # False
Enter fullscreen mode Exit fullscreen mode

Floats are immutable but not cached.


3️⃣ Complex Numbers

a = 10 + 5j
b = 10 + 5j
print(a is b)   # False
Enter fullscreen mode Exit fullscreen mode

Complex numbers are never cached.


4️⃣ Mutable Objects

a = []
b = []
print(a is b)   # False
Enter fullscreen mode Exit fullscreen mode

Includes:

  • list
  • dict
  • set
  • bytearray
  • class instances

Each mutable object gets separate memory.


5️⃣ Tuples (Special Case)

a = (1, 2)
b = (1, 2)
print(a is b)   # Usually False
Enter fullscreen mode Exit fullscreen mode

When tuples may be cached:

  • empty tuple ()
  • compile-time constants ⚠️

But do not rely on it.


🔷 Summary Table

Object Type Cached?
Small integers (-5 to 256) ✅ Yes
Large integers ❌ No
Boolean (True, False) ✅ Yes
None ✅ Yes
Short/identifier strings ⚠️ Sometimes
Float ❌ No
Complex ❌ No
Empty tuple ✅ Yes
List / Dict / Set ❌ No

🔷 Why Python Uses Caching

Python caches objects to:

  • reduce memory usage
  • improve performance
  • safely reuse immutable objects

Because immutables cannot change, sharing them is safe.


⭐ Golden Rule

Use == for value comparison.
Use is only for identity checks (None, True, False).


🎯 Interview One-Liner

Python caches small immutable objects like small integers, booleans, and None for performance, but does not cache floats, complex numbers, or mutable objects.


Top comments (0)