Understanding Python's small integer caching mechanism
This is an easy Python language-knowledge question that tests whether you understand how the interpreter optimises memory and object reuse under the hood. It probes your familiarity with Python's runtime behaviour—the kind of detail that surfaces in code reviews, debugging sessions, and deeper technical conversations.
Python caches certain immutable objects to improve performance and reduce memory overhead. Small integers are a canonical example: rather than creating a new object each time a literal or expression evaluates to a common small integer, the interpreter reuses a pre-allocated pool. Knowing this range matters when reasoning about object identity (the is operator) versus equality (the == operator), and helps explain why identity checks sometimes behave counterintuitively in interactive sessions versus scripts.
- Object identity vs. equality in Python
- Immutable object pooling and interning
- How interpreter optimisations affect is-comparisons