0
0
PythonConceptBeginner · 3 min read

What is Interning in Python: Explanation and Examples

In Python, interning is a method of storing only one copy of immutable objects like strings to save memory and speed up comparisons. When strings are interned, Python reuses the same object in memory instead of creating duplicates.
⚙️

How It Works

Imagine you have many copies of the same book in a library. Instead of storing each copy separately, you keep just one copy and let everyone read from it. This is similar to how interning works in Python for strings and some immutable objects.

When Python interns a string, it keeps a single copy of that string in a special place called the intern pool. If the same string is needed again, Python uses the existing copy instead of making a new one. This saves memory and makes comparing strings faster because Python can just check if two variables point to the same object.

Interning happens automatically for some strings, like short strings or identifiers, but you can also manually intern strings using the sys.intern() function.

💻

Example

This example shows how interning makes two identical strings share the same memory location, making comparison faster.

python
import sys

# Two identical strings created separately
str1 = 'hello world'
str2 = 'hello world'

# Check if they are the same object (memory location)
print(str1 is str2)  # Usually True because of string interning for literals

# Intern the strings
str1 = sys.intern(str1)
str2 = sys.intern(str2)

# Now they point to the same object
print(str1 is str2)  # True
Output
True True
🎯

When to Use

Interning is useful when you have many identical immutable objects, especially strings, and want to save memory or speed up equality checks. For example, if you process large text data with repeated words or identifiers, interning can reduce memory usage.

It is also helpful in performance-critical code where string comparisons happen frequently, as comparing object identities is faster than comparing string contents.

However, interning should be used carefully because it adds overhead to manage the intern pool and is only beneficial when many duplicates exist.

Key Points

  • Interning stores one copy of immutable objects like strings to save memory.
  • Python automatically interns some strings, but you can manually intern using sys.intern().
  • Interned strings share the same memory location, making comparisons faster.
  • Useful for large datasets with many repeated strings or performance-critical code.
  • Use interning carefully to avoid unnecessary overhead.

Key Takeaways

Interning stores one copy of immutable objects to save memory and speed up comparisons.
Use sys.intern() to manually intern strings in Python.
Interned strings share the same memory location, making identity checks faster.
Interning is helpful for large datasets with many repeated strings.
Avoid overusing interning to prevent extra overhead.