I want to check the size of int data type in python:
import sys
sys.getsizeof(int)
It comes out to be “436”, which doesn’t make sense to me.
Anyway, I want to know how many bytes (2,4,..?) int will take on my machine.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The short answer
You’re getting the size of the class, not of an instance of the class. Call
intto get the size of an instance:If that size still seems a little bit large, remember that a Python
intis very different from anintin (for example) C. In Python, anintis a fully-fledged object. This means there’s extra overhead.Every Python object contains at least a refcount and a reference to the object’s type in addition to other storage; on a 64-bit machine, just those two things alone take up 16 bytes! The
intinternals (as determined by the standard CPython implementation) have also changed over time, so that the amount of additional storage taken depends on your version.intobjects in CPython 3.11Integer objects are internally
PyLongObjectC types representing blocks of memory. The code that defines this type is spread across multiple files. Here are the relevant parts:If we expand all the macros and replace all the
typedefstatements, this is the struct we end up with:uint32_tmeans "unsigned 32-bit integer" anduint32_t ob_digit[1];means an array of 32-bit integers is used to hold the (absolute) value of the integer. The "1" in "ob_digit[1]" means the array should be initialized with space for 1 element.So we have the following bytes to store an integer object in Python (on a 64-bit system):
Py_ssize_t, signed) forob_refcnt– the reference countPyTypeObject*) forob_type– the pointer to theintclass itselfPy_ssize_t, signed) forob_size– which stores how many 32-bit integers are used to store the integerand finally a variable-length array (with at least 1 element) of
The comment that accompanies this definition summarizes Python 3.11’s representation of integers. Zero is represented not by an object with size (
ob_size) zero (the actual size is always at least 1 though). Negative numbers are represented by objects with a negative size attribute! This comment further explains that only 30 bits of eachuint32_tare used for storing the value.On CPython 3.10 and older,
sys.getsizeof(0)incorrectly returns 24 instead of 28, this was a bug that was fixed. Python 2 had a second, separate type of integer which worked a bit differently, but generally similar.You will get slightly different results on a 32-bit system.