Internals of a Variable in Python: What Happens Under the Hood?

Posted by Afsal on 03-Jan-2025

Hi Pythonistas!

Variables are fundamental to any programming language, and Python's implementation of variables is both elegant and unique. Unlike many other languages, Python doesn’t treat variables as traditional memory locations. Instead, it leverages a powerful abstraction model that makes working with variables intuitive yet efficient. In this post, we’ll dive deep into the internals of variables in Python to understand how they work.

What Is a Variable in Python?

In Python, a variable is a name that acts as a reference or label to an object stored in memory. Unlike lower-level languages like C, Python variables do not directly hold values or represent specific memory locations. Instead, variables are pointers to objects.

Key Concept:

  • Variables in Python are references to objects, not containers.
  • Every object in Python has a unique identity (memory address), a type, and a value.

Objects and IDs

When you create a variable in Python, it points to an object in memory. The id() function in Python returns the memory address (identity) of an object.

code

>>> 
>>> x = 42
>>> id(x)
128368683795984
>>> 

The variable x doesn’t store 42 itself. Instead, it stores a reference to the object representing the integer 42 ie 128368683795984.

 Dynamic Typing

Python is dynamically typed, meaning variables can reference objects of different types at runtime.

 code

x = 42  
x = "Hello" 

In this example:

When x = 42, x references an integer object.
When x = "Hello", the reference to 42 is discarded, and x now points to a string object.

Variable Internals: The PyObject Structure

Internally, every object in Python is represented by a structure called PyObject. Here's what it contains:

Reference Count (ob_refcnt): Tracks how many variables are referencing the object. When the reference count drops to zero, the object is garbage collected. We have post on how reference counting works please check if you need more clarity

Type (ob_type): Specifies the type of the object (e.g., integer, string, list).Helps Python determine what operations are valid on the object.
Value (ob_value): Contains the actual data or value of the object.

code

x = 10
y = x

When x = 10: Python creates an integer object 10 in memory. x references this object. When y = x: Python does not create a new object. Instead, y references the same object as x. You can confirm this with the id() function

code

>>> x = 1
>>> y = x
>>> id(x)
136417020739824
>>> id(y)
136417020739824
>>> 

Advantages of Python’s Variable Model

Flexibility: Dynamic typing allows for easy prototyping and development.
Memory Efficiency: Shared references avoid unnecessary duplication.
Garbage Collection: Automatic memory management reduces the burden on developers.

Python's variable system is designed to make programming intuitive and efficient. By abstracting away memory management and leveraging references, Python provides a high-level, user-friendly approach to handling data. Understanding these internals not only improves your debugging skills but also helps you write more optimized and effective Python code.

We have planning more posts on internals. Please share your valuable suggestions with afsal@parseltongue.co.in