The real memory contents of eight objects, taken with ctypes.string_at(id(obj), sizeof) and broken down into fields.
A mechanism described in words and a mechanism you have turned over in your hands settle differently. Below is not an illustration but a dump: every byte was taken from a live process, every field checked against a known value (the count, the length, the hash, the type pointer). Hover over a field in the table or tap it, and the corresponding bytes light up.
Everything shown here is captured with five lines. Paste them into a terminal or a notebook as they are:
import ctypes, sys, struct
obj = 2**70
n = sys.getsizeof(obj)
raw = ctypes.string_at(id(obj), n)
print(n, raw.hex(' '))
print("refcnt:", struct.unpack_from("<q", raw, 0)[0])
print("type is int:", struct.unpack_from("<Q", raw, 8)[0] == id(int))
print("ob_size:", struct.unpack_from("<q", raw, 16)[0])
print("digits:", [struct.unpack_from("<I", raw, 24 + 4*i)[0] for i in range(3)])
Careful: string_at reads raw memory at an address. A mistake in the length means a segfault of the whole process, not an exception. This is exactly the boundary beyond which Python stops being a safe language.
int it is 24 bytes against 4 useful ones, for the string “hello” 48 against 5. The single object model is paid for here, in bytes, on every object.ob_item field is the address of another block. Hence the cache misses on traversal, and hence numpy.__slots__ turns an object into a C struct. An attribute at a fixed offset instead of a walk through dictionaries: 56 bytes per instance against 96, and 160 if anyone in the code has touched __dict__.wstr field of strings, which takes eight bytes here, was simply removed in 3.12.