Python Track
Python OverviewPython 3.12
Beginner Tier8 Ch
CPython Execution Architecture & Bytecode Compilation
Variables as Name Bindings & Reference Pointers
Intermediate Tier8 Ch
Hard Tier8 Ch
Progress0 / 48 (0%)
Python 3.12+
Chapter 1 — Introduction to Python & The CPython ArchitectureCPython Execution Architecture & Bytecode Compilation
Beginner·Chapter 1 — Introduction to Python & The CPython Architecture

CPython Execution Architecture & Bytecode Compilation

How Python source code (.py) is tokenized, parsed into an Abstract Syntax Tree (AST), compiled into bytecode (.pyc), and executed by the CPython virtual evaluation loop.

“Python is an interpreted, bytecode-compiled language. CPython tokenizes source, constructs an AST, compiles bytecode, and executes opcodes sequentially in ceval.c.”

CPython Systems Architecture

The CPython Compilation & Execution Pipeline

Step 1 of 6
Human Readable

1. Python Source Code

Target: main.py

Developers author human-readable Python script adhering to PEP 8 idioms and Python 3.12+ syntax rules.

Architectural Invariants & Rules
  • Unicode UTF-8 plain-text source files (.py)
  • Dynamic type hints (PEP 484) validated at development time
  • Indentation-driven structural blocks
Runtime Representation Preview
def compute_sum(a: int, b: int) -> int:
    result = a + b
    return result

print(compute_sum(40, 2))

Step-by-Step Program Execution & Memory State Tracing

Step 01: Function Definition & Code Object GenerationLine 5

The compiler constructs a PyCodeObject containing bytecode instructions, constant tuples (co_consts), and local variable names (co_varnames).

Step 02: Operand Push to Evaluation StackLine 6

LOAD_FAST pushes local variable 'n' onto the CPython execution frame's value stack twice.

Step 03: Binary Operation ExecutionLine 6

BINARY_OP pops the top two values, multiplies them via the PyNumber_Multiply C API, and pushes the product.

Step 04: Frame Unwinding & ReturnLine 7

RETURN_VALUE pops the product from the stack and hands it back to the caller frame.

Idiomatic Python Code Implementation

cpython-architecture.py
CPython 3.12
# Exploring Python Bytecode via the standard 'dis' disassembler module
import dis

def compute_square(n: int) -> int:
    result = n * n
    return result

print("=== Python Function Disassembly ===")
dis.dis(compute_square)
Standard Output (stdout)
exit code: 0
$python3 cpython-architecture.py
=== Python Function Disassembly ===
  5           0 RESUME                   0

  6           2 LOAD_FAST                0 (n)
              4 LOAD_FAST                0 (n)
              6 BINARY_OP                5 (*)
             10 STORE_FAST               1 (result)

  7          12 LOAD_FAST                1 (result)
             14 RETURN_VALUE

Staff-Level & FAANG Technical Interview Questions

Is Python an interpreted or compiled language?
GoogleMetaAmazon

Python is both. Source code is first compiled into intermediate bytecode (.pyc), which is then interpreted by the CPython virtual machine execution loop (ceval.c). Implementations like PyPy use JIT (Just-In-Time) compilation to emit native machine code at runtime.

What is the role of the PEG parser introduced in Python 3.9?
NetflixApple

Python 3.9 replaced the legacy LL(1) parser with a Parsing Expression Grammar (PEG) parser. PEG allows arbitrary lookahead, eliminating grammatical ambiguities and enabling modern syntax features like structural pattern matching (match/case).

Checkpoint Knowledge Verification

What does the CPython compiler emit before running code through the evaluation loop?

Primary Specifications & Official References

Python 3 dis Module DocumentationPEP 617 — New PEG Parser for CPython