System Atlas
the atlas

How Python works

From a .py file to running instructions — and why the GIL shapes everything.

the gist

Python source is compiled to compact bytecode, which the CPython virtual machine executes one instruction at a time. A Global Interpreter Lock lets only one thread run bytecode at once, memory is reclaimed by reference counting plus a cycle collector, and the language's dynamic, interpreted nature trades raw speed for flexibility.

ANALOGY // Think of a chef (the VM) reading a recipe card (bytecode) one line at a time, where a single shared knife (the GIL) means only one cook can chop at once even in a crowded kitchen.
living diagram01 / 06
source.pyhuman-readableCompilerCPython frontendBytecode.pyc · __pycache__Virtual machineeval loopGILone thread runsRef count + GCfrees memory
01

You write source code

You write human-readable instructions in a .py file using Python's dynamic syntax, where variables have no fixed type. This text is not something the CPU can run directly — it first has to be translated into a form the interpreter understands. Nothing about types or names is checked until the code actually runs.

02

Compile to bytecode

When you run the file, CPython compiles the source into bytecode — a compact, platform-independent set of low-level instructions like LOAD_FAST and BINARY_ADD. This compiled form is cached in .pyc files inside __pycache__ so unchanged modules can skip recompilation next time. Bytecode is an intermediate representation, not native machine code.

03

The virtual machine runs it

The Python Virtual Machine, inside the CPython interpreter, executes the bytecode in a giant evaluation loop that fetches one instruction, does what it says, and moves to the next. Because a program is understood and run instruction by instruction rather than compiled ahead of time to native code, Python is an interpreted language. This indirection is the main reason it runs slower than C or Java.

04

The GIL serializes threads

A Global Interpreter Lock protects the interpreter's internal state, and it lets only one thread execute Python bytecode at any given moment. This means CPU-bound work spread across threads does not actually run in parallel on multiple cores — the threads take turns. To use many cores for computation you reach for multiple processes instead of threads.

05

I/O releases the lock

Crucially, a thread releases the GIL whenever it blocks on I/O — reading a file, waiting on a network call, or sleeping. While one thread waits, another can run bytecode, so threads and async code deliver real concurrency for I/O-bound workloads. The GIL only hurts when the bottleneck is computation, not waiting.

06

Memory is reclaimed

Every object carries a count of how many references point to it; when that count drops to zero the object is freed immediately. Reference counting alone cannot reclaim objects that reference each other in a cycle, so a separate cyclic garbage collector periodically finds and clears those. Together they keep memory management automatic and mostly invisible to you.

the full picture
01

Why Python works the way it does

Python was designed to make programmers productive: code that reads almost like English, no compile-and-link ceremony, and no need to declare the type of every variable. Achieving that ease means the language does a lot of work at run time that other languages do ahead of time, and understanding how Python actually runs your code explains both its famous friendliness and its equally famous performance quirks.

The reference implementation everyone means when they say "Python" is CPython, written in C. When you type python script.py, CPython does not hand your file to the CPU. Instead it runs your program through two stages — a quick compilation to an intermediate form, then interpretation of that form by a virtual machine. Almost every surprising thing about Python's speed, its threading model, and its memory behavior falls out of this two-stage design.

02

From source to bytecode

The first stage is compilation, but not to machine code. CPython parses your .py file into a syntax tree and then compiles that into bytecode — a compact, platform-independent sequence of simple instructions such as LOAD_FAST, BINARY_ADD, and RETURN_VALUE. You can see it yourself with the dis module, which disassembles any function into its bytecode.

Bytecode is an intermediate representation: lower-level than your source but still not something a processor runs directly. To avoid recompiling unchanged code, CPython caches the bytecode for imported modules in .pyc files inside a __pycache__ directory, so the next run can skip straight to execution. This compile step is fast and happens transparently, which is why Python feels like it "just runs" your source with no separate build command — the build is simply hidden inside startup.

03

The virtual machine that runs it

The second stage is the one that gives Python its character. The Python Virtual Machine inside CPython is essentially a huge loop — the evaluation loop — that walks through the bytecode one instruction at a time: fetch the next instruction, decode what it means, perform it against a stack of values, repeat. Because your program is understood and executed step by step rather than compiled to native code up front, Python is an interpreted language.

This is also where Python's dynamic nature lives. Since types are attached to objects rather than variables, the VM figures out at run time what a + b means based on what a and b actually are at that moment. That flexibility — duck typing, monkey-patching, adding attributes on the fly — is enormously convenient, but it forces the interpreter to do type checks and lookups on every operation. That per-instruction overhead is the core reason CPython is typically much slower than an ahead-of-time compiled language like C, and why performance-critical code often drops into C extensions or tools like NumPy.

04

The GIL: one thread at a time

Threads in CPython are real operating-system threads, but a single lock governs the interpreter: the Global Interpreter Lock (GIL). The GIL guarantees that only one thread executes Python bytecode at any instant. It exists because CPython's memory management — reference counting in particular — is not thread-safe, and a single coarse lock was a simple, fast way to keep the interpreter correct.

The consequence is the most misunderstood fact about Python. For CPU-bound work — number crunching, tight loops — spreading it across threads does *not* use multiple cores, because the threads must take turns holding the GIL; they run concurrently but never truly in parallel. To use many cores for computation you use multiprocessing (separate processes, each with its own interpreter and GIL) instead of threads.

But for I/O-bound work the story flips. Whenever a thread blocks on I/O — a network request, a disk read, a sleep — it *releases* the GIL so another thread can run. So threads and asyncio deliver excellent concurrency for programs that spend most of their time waiting. The GIL only hurts when your bottleneck is the CPU, not the wait.

05

How memory is reclaimed

Python manages memory for you through two cooperating mechanisms. The primary one is reference counting: every object keeps a count of how many references point at it, incremented when you assign it somewhere and decremented when a reference goes away. The instant that count hits zero, the object is unreachable and its memory is freed immediately. This is simple and predictable, and it reclaims most garbage the moment it becomes garbage.

Reference counting has one blind spot: reference cycles. If object A refers to B and B refers back to A, their counts never fall to zero even when nothing else can reach them. To catch this, CPython runs a separate cyclic garbage collector periodically that detects and clears such unreachable cycles. The two together mean you almost never manage memory by hand — though understanding them explains real behaviors like why holding references keeps large objects alive, and why the cycle collector occasionally does non-trivial work.

06

What this means in practice

Put together, the pipeline is: your dynamic, human-friendly source is compiled to bytecode, the CPython virtual machine interprets that bytecode instruction by instruction under the protection of the GIL, and reference counting plus a cycle collector keep memory tidy underneath. Each design choice trades machine efficiency for developer speed.

The practical guidance follows directly. Reach for threads or async when your program waits on I/O, and for multiprocessing or C-backed libraries when it grinds the CPU. Don't be surprised that pure-Python loops are slow — push hot numeric work into NumPy or a compiled extension. And remember that "Python" usually means CPython specifically: alternative implementations like PyPy (with a just-in-time compiler) or GIL-removal efforts change these trade-offs, which is exactly why knowing the underlying model, rather than memorizing rules, is what lets you reason about performance.

what to remember
01

Source .py is compiled to bytecode (.pyc), not machine code — an intermediate the interpreter understands.

02

The CPython virtual machine is a loop that fetches and executes bytecode instructions one at a time.

03

The GIL allows only one thread to run Python bytecode at any instant, so CPU-bound threads can't truly parallelize.

04

For I/O-bound work the GIL is released during waits, so threads and async still help a lot.

05

Memory is freed by reference counting, with a separate cyclic garbage collector to catch reference cycles.

06

Python is dynamically typed and interpreted: flexible and fast to write, slower at runtime than compiled languages.

check yourself

Quick quiz

Q1

What does CPython actually execute?

Q2

Why doesn't threading speed up CPU-bound Python code?

follow the thread