Why is Python Integer Size Different from Integers in C?

Unlike C, Python integers are objects that use an internal data structure for storage. It’s completely different from the basic arithmetic types such as integers or real numbers. This is what makes a Python integer size diffrent from other languages.

In the next few lines, we’ll explain this concept in detail i.e. why is Python integer size bigger than the integers in C?

Why is Python Integer Size Different from Integers in C?

Whenever Python finds an assignment like <x=1>, it tries to construct an integer object. In Python, an integer object is seeded using the following data structure.

typedef struct {
    long ob_ival;
} PyIntObject;


Python Integer-PyIntObject List


The <PyObject_HEAD> is macro which expands to a reference counter of type <Py_ssize_t> and a pointer of <PyTypeObject> type.

Py_ssize_t ob_refcnt;
PyTypeObject *ob_type;


Next, Python reserves a pre-allocated block of Integer objects. And use it to serve new integer request instead of making allocations on every assignment. Here is the block structure used to hold integer objects.

struct _intblock {
    struct _intblock *next;
    PyIntObject objects[N_INTOBJECTS];
typedef struct _intblock PyIntBlock;


You can verify from the above structure that it can hold up to 40 PyIntObject objects on a 64-bit system. It’s the maximum of integer objects that can fit in a block of 1K bytes.

#define N_INTOBJECTS    ((BLOCK_SIZE - BHEAD_SIZE) / sizeof(PyIntObject))

Once a block’s integer object pool gets depleted, a new block becomes available to fulfill the new requests for integer objects. All these blocks are chained together in the form of a single linked list.



We should also mention that Python handles small integers in a little different fashion than big integers. It uses a <PyIntObject> type array of 262 pointers to store the small integers falling in the range from -5 to 256.

static PyIntObject *small_ints[NSMALLNEGINTS + NSMALLPOSINTS];

It means the small integers won’t get served from the object pool. Instead, they will use the list of pointers given above. Hence, the small integers will end up using the same PyIntObject integer object but with a different reference count.

On the contrary, the big integers get their allocation from the integer object pool. And each of them would have its own <PyIntObject> type object.

Another interesting fact which you should know is the difference between the lifetime of small and big integers. Unlike a big integer, small integers stay in memory as long as the Python interpreter is running. Observe this fact from the example given below.

=> None
=> None
   small_int1 is small_int2
=> True
=> None
=> None
   big_int1 is big_int2
=> False


Final Thoughts.

Till now, you’ve learned that Python integers are entirely different from the integer type available in C. And more importantly, you would now know the answer to your question- “Why is Python Integer Size Different from Integers in C?”.

We hope you had enjoyed this post and have a clear idea of the integer object pool, its structure, and application in Python.





  1. jawahar Reply
    • Meenakshi Agarwal Reply