DEV Community

Cover image for Python Object Interning
Rajesh
Rajesh

Posted on

Python Object Interning

This is an interesting thing i have noticed recently asked in an interview...

Object interning

is a technique used in Python to optimize memory usage and improve performance by reusing immutable objects. In Python, objects such as integers (-5 to 256), strings, and tuples are immutable, meaning their values cannot be changed after they are created. Object interning takes advantage of this immutability to reuse objects with the same value rather than creating new ones, thus saving memory and speeding up certain operations.

Here's why object interning is useful along with some examples in Python:

  1. Memory Optimization:

When Python interns objects with the same value, it ensures that only one copy of the object exists in memory. This saves memory, especially when dealing with large numbers of small immutable objects.

   a = 10
   b = 10
   print(a is b)  # True, since a and b point to the same memory location
Enter fullscreen mode Exit fullscreen mode
  1. Performance Improvement:

Object interning can improve the performance of equality comparisons (e.g., ==) and identity comparisons (e.g., is) by comparing memory addresses directly rather than comparing values.

   a = 1000
   b = 1000
   print(a is b)  # False, since a and b are different objects
   print(a == b)  # True, since their values are equal
Enter fullscreen mode Exit fullscreen mode
  1. Caching Immutable Objects:

Python caches small integers (-5 to 256) and some strings, which are commonly used, to further optimize memory usage and performance.

   a = 100
   b = 100
   print(a is b)  # True, since a and b are cached and share the same memory
Enter fullscreen mode Exit fullscreen mode
  1. String Interning:

Python interns small strings (length <= 20 characters) automatically. However, you can manually intern strings using the intern() function from the sys module.

   import sys

   a = "hello"
   b = "hello"
   print(a is b)  # True, since a and b are interned
Enter fullscreen mode Exit fullscreen mode
   a = sys.intern("hello")
   b = sys.intern("hello")
   print(a is b)  # True, since a and b are explicitly interned
Enter fullscreen mode Exit fullscreen mode

Overall, object interning is a powerful optimization technique in Python that helps reduce memory consumption and improve the performance of certain operations, especially when dealing with immutable objects like integers and strings. However, it's important to be aware of its limitations and use it judiciously, as not all objects can be interned, and excessive interning can lead to unintended memory leaks.

Image of Datadog

Master Mobile Monitoring for iOS Apps

Monitor your appโ€™s health with real-time insights into crash-free rates, start times, and more. Optimize performance and prevent user churn by addressing critical issues like app hangs, and ANRs. Learn how to keep your iOS app running smoothly across all devices by downloading this eBook.

Get The eBook

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

๐Ÿ‘‹ Kindness is contagious

Please leave a โค๏ธ or a friendly comment on this post if you found it helpful!

Okay