In C and many other languages an integer is a fixed 4 or 8 bytes, and going past overflows. A Python int is a full object that grows as needed, so it never overflows (until you run out of memory). That convenience costs memory and speed, and it comes with a few behaviors that trip people up in interviews. This note walks through how an int is stored, why that makes it safe and slow, and which edge cases to watch for.
How an int is stored#
"No overflow" starts with the layout. CPython stores an int as a PyLongObject, which looks roughly like this C struct:
struct PyLongObject {
Py_ssize_t ob_refcnt; // reference count
PyTypeObject *ob_type; // points to the int type
Py_ssize_t ob_size; // digit count (also holds the sign)
digit ob_digit[1]; // the digits, least significant first
};
(Python 3.12 packs size and sign into one tag field, but the idea is the same.)
The number lives in ob_digit as an array of "super-digits" in base on 64-bit builds. Each digit holds a value from 0 up to about one billion. Here's how a few values map onto that array:
| Value | ob_digit |
Digits used |
|---|---|---|
1789 |
[1789] |
1 |
2**30 + 5 |
[5, 1] (value ) |
2 |
2**1000 |
34 digits | 34 |
Big numbers use a longer digit array. That's the whole trick behind "no overflow".
The small-integer cache#
Since every int is a full object, creating a fresh one for every 0, 1 or -1 would be wasteful. So CPython creates the integers -5 to 256 once at startup and reuses them.
You can see the cache with is, which checks whether two names point at the same object:
a = 256
b = 256
print(a is b) # True: both point at the cached 256
c = int("257") # built at runtime, outside the cache
d = int("257")
print(c is d) # False: two separate objects
print(c == d) # True: same value
If you type c = 257; d = 257 in one script, c is d may print True, because the compiler can reuse one constant within the same code block. That is an implementation detail. Always compare numbers with ==, never is.
Since Python 3.12, cached ints are "immortal": their refcount is never decremented, so they are never freed.
Ints are immutable#
The cache is safe to share because ints can't change. Arithmetic always produces a result object, and the name moves to it, as id() shows here:
x = 1789
before = id(x)
x = x + 1
print(id(x) == before) # False: x now names a different object
Here's what x = x + 1 does step by step:
- Read the C value out of the
1789object and add 1 (plain C arithmetic). - If the result is in -5..256, return the cached object. Otherwise allocate a new int object holding
1790. - Point
xat the result. - Decrement the old object's refcount. If nothing else uses
1789, it is freed.
Whether step 2 allocates depends on the result, as these examples show:
| Expression | New object allocated? | Why |
|---|---|---|
2 + 3 |
No | 5 comes from the cache |
1000 + 2000 |
Yes | 3000 is outside the cache |
my_list.append(5) |
No new list | Lists are mutable, same object grows |
What ints cost#
Being a full object makes an int flexible, and you pay for that in two ways.
Memory. A small int takes about 28 bytes (header + size + one digit), versus 4 bytes in C. You can check it with sys.getsizeof:
import sys
print(sys.getsizeof(1)) # 28 on a typical 64-bit build
print(sys.getsizeof(2**30)) # 32: needs a second digit
A list of one million distinct large ints is roughly 28 MB of int objects plus 8 MB of list pointers, not the 4 MB you'd expect in C. If memory matters, array.array('i', ...) stores raw C ints.
Time. Operations on small ints are effectively . On huge ints they scale with the number of digits :
| Operation | Cost on big ints |
|---|---|
+, -, comparison |
, like column addition on paper |
* |
Karatsuba for large operands, about |
str(n) / int(s) |
Superlinear; Python 3.11+ refuses over 4300 digits by default (sys.set_int_max_str_digits) |
So "arithmetic is " is a fair assumption in interviews only while numbers stay bounded.
Interview gotchas#
Integer behavior that differs from other languages often shows up in interview edge cases. The snippet below collects the ones that come up most: rounding of // and %, bool as an int, negative shifts, and a few built-ins.
# Floor division and modulo round toward negative infinity
print(-7 // 2) # -4 (C gives -3)
print(int(-7 / 2)) # -3: truncates toward zero ("Evaluate RPN")
print(-7 % 3) # 2: result takes the sign of the divisor
# bool is a subclass of int
print(True + True, isinstance(True, int)) # 2 True
# Negative numbers act like infinite two's complement
print(-1 >> 1) # -1: right shift never reaches 0
MASK = 0xFFFFFFFF # simulate 32-bit ints in bit problems
print((-1) & MASK) # 4294967295
# Handy built-ins
print(int("101", 2), bin(5)) # 5 '0b101'
print((5).bit_count()) # 2 (bit_count needs 3.10+)
print(float("inf") > 10**100) # True: a safe "infinity" sentinel
These behaviors lead to three habits:
- No overflow means you never need max-int overflow checks, but problems that say "assume 32-bit integers" (e.g. Reverse Integer) expect you to clamp yourself.
- Bit tricks with negatives need a mask, since Python has no fixed width.
- Use
math.isqrt(n)for exact integer square roots instead ofint(n ** 0.5), which can be off by one for largen.
Key takeaways#
- A Python int is an object holding an array of base- digits, so it never overflows.
- -5 to 256 are cached singletons; compare numbers with
==, notis. - Ints are immutable: every arithmetic result is a (possibly cached) different object.
- Each int costs about 28 bytes, and big-number arithmetic scales with digit count.
//and%floor toward negative infinity, andboolis anint.