Two 257s, One Object

This runs under CPython, as a script (not typed interactively at the REPL).

a = 257
b = 257
print(a is b)

A teammate sees this print True and concludes that CPython must cache all integers up to at least 257, not just the documented -5 to 256 range. What's actually going on, and why is their conclusion wrong?

A

This actually prints False. 257 falls outside CPython's -5 to 256 cache range, and CPython always allocates a fresh object for any int outside that range, regardless of how it's written.

B

a and b are the same literal 257 written twice in the same file. CPython's compiler deduplicates identical constant literals at compile time, so both names end up bound to one pre-built int object, a compile-time artifact of this specific source, not evidence about the runtime int cache. Building 257 two separate ways at runtime instead (for example, one computed inside one function call and another computed inside a different one) shows is returns False, confirming the real cache still ends at 256.

C

CPython's small-int cache grows dynamically: the first time any code uses the literal 257, it gets added to the permanent cache for the rest of the process, so any later 257 anywhere in the program will also satisfy is.

D

is and == are defined identically for int, since int is immutable, so any two equal-valued ints will always satisfy is, independent of caching.