DEV Community

Cover image for Python's Integer Interning Trap: Why 'is' Lies Above 256
Binary Dose
Binary Dose

Posted on Originally published at binarydose.in

Python's Integer Interning Trap: Why 'is' Lies Above 256

Python's Integer Interning Trap

a = 256
b = 256
print(a is b)  # True

x = 257
y = 257
print(x is y)  # False (in REPL!)
Enter fullscreen mode Exit fullscreen mode

⚡ The Small Integer Cache

CPython allocates a fixed table of integer objects spanning [-5, 256] during interpreter initialization.

Because integers are immutable, caching frequently used numbers saves massive memory overhead.

Read the full breakdown on Binary Dose.

Top comments (0)