Onkar Lapate’s Post

In C, an integer takes 4 bytes. In Python, the number 1 takes 28 bytes. Why? As python is a "Dynamically Typed" language, it needs to store more than just the value 1. It needs to store "metadata" about that value so the interpreter knows what to do with it. How? In the CPython source code, every single thing is a PyObject. So when we create x = 1, following structure is created with it: 1. ob_refcnt (8 bytes): The Reference Counter. It tracks how many variables point to this object. 2. ob_type (8 bytes): A pointer to the "Type Object" (telling Python the datatype). 3. ob_size (8 bytes): For variable-sized objects (like lists) 4. The Actual Value (4-8 bytes): The actual number 1 Understanding this overhead explains why Python is memory-intensive being a dynamically typed language. I am trying to learn Python Internals in detail and will share my learnings. Do follow along and tell your experiences in comments. #Python #PythonInternals #SoftwareEngineering #BackendDevelopment

  • diagram

To view or add a comment, sign in

Explore content categories