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

2026-10-27来源 dev.to PythonEnglish

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!) 

⚡ 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.

原文:https://www.a8q9.cn/en/article/python-s-integer-interning-trap-why-is-lies-above-256-91c4b2.html

相关文章