>>> sys.getsizeof(int)
436 #? does this mean int occupies 436 bytes .
>>> sys.getsizeof(1)
12 #12 bytes for int object, is this the memory requirement.
I thought int in python is represented by 4 bytes, why is it reporting 12 bytes
Please someone explain why is it reporting 12 bytes when int uses just 4 bytes
Yes, an
intinstance takes up 12 bytes on your system. Integers (like any object) have attributes, i.e. pointers to other objects, which take up additional memory space beyond that used by the object’s own value. So 4 bytes for the integer’s value, 4 bytes for a pointer to__class__(otherwise, Python wouldn’t know what type the object belonged to and how to start resolving attribute names that are inherited from theintclass and its parents), and another 4 for the object’s reference count, which is used by the garbage collector.The type
intoccupies 436 bytes on your system, which will be pointers to the various methods and other attributes of theintclass and whatever other housekeeping information Python requires for the class. Theintclass is written in C in the standard Python implementation; you could go look at the source code and see what’s in there.