PyDSAWHY Engine
Back to Roadmap
Track 02 of 10
Memory & HardwareIntermediate Level~25 Mins

2. Python Memory Model & Pointers Under The Hood

Demystify id(), PyObject structs, heap allocation, and mutable vs immutable references.

The "WHY" Core Principle

In Python, EVERYTHING is an object allocated on the Heap (`PyObject`). Variable names in Python are reference arrows pointing to 64-bit memory addresses returned by `id()`.

Interactive Visualizer

Python RAM Memory Model & Reference Pointer Simulator

Observe how Python variable names in Stack Frames point to 64-bit PyObject pointers on the Heap.

1. Stack Frame (Variable Names)Local Scope
var a
Address Pointer:0x7F9A140
var b
Address Pointer:0x7F9A140
2. Heap Memory (PyObject Memory Slots)64-bit Virtual RAM
PyListObject @ 0x7F9A140Referenced by 'a' & 'b'
ob_item:[10, 20, 30]
WHY THIS MATTERS: Executing 'b = a' simply copied the 64-bit pointer address 0x7F9A140. No elements were duplicated!

Interactive Code Snippets (1 Lessons)

Lesson 01

Variables are Pointers: Understanding id()

O(1)O(1)

Inspect actual 64-bit RAM memory addresses of variables.

WHY Under The Hood:

Assigning `b = a` does NOT copy values or allocate new memory. It simply copies the 64-bit memory address pointer from variable `a` into variable `b`.

CPython C Struct Detail: In CPython, `id(obj)` returns the exact 64-bit virtual memory address of the `PyObject` structure.

Variables are Pointers: Understanding id()

Pyodide WASM Engine

Type, edit code, and click 'Run & Profile' to see live runtime ms and operation count!

Python 3.12 Code (Editable)
Expected:Time: O(1)Space: O(1)

Test Your Understanding

When you execute `b = a` where `a = [1, 2, 3]`, what happens in memory?