Under The Hood
backend python CPython 3.12+

CPython Bytecode: .pyc Files, dis Disassembly, and the Eval Frame Loop

Last updated
Prerequisites:
Python Syntax and AST
Stack-based Virtual Machines
  • python
  • cpython
  • bytecode
  • dis-module
  • pyc
  • interpreter
  • eval-frame

Read at your depth

The practical view

When Python imports a module, it compiles the source text into **Bytecode** and saves it in the `__pycache__/*.pyc` directory. You can inspect compiled bytecode instructions using the standard library `dis` module: `import dis; dis.dis(my_func)`. Common opcodes include `LOAD_FAST` (load local variable), `LOAD_GLOBAL`, `STORE_FAST`, `CALL`, and `RETURN_VALUE`. CPython executes bytecode using an internal stack-based virtual machine.

The same idea in other frameworks

python equivalent
CPython bytecode disassembler (`dis.dis()`) on stack-based VM.

Legacy vs modern

Python Source Code vs Compiled Bytecode Disassembly

Simple arithmetic compiles into stack-based bytecode instructions; `dis` reveals opcode specialization and local variable fastpaths.

before → after
High-Level Python Function
def add(a, b): return a + b
Compiled Bytecode Disassembly (dis output)
LOAD_FAST 0 (a) -> LOAD_FAST 1 (b) -> BINARY_OP 0 (+) -> RETURN_VALUE

Interview gotchas

Context

Interviewers test your knowledge of CPython's stack frame optimization.

The mechanical answer

`LOAD_FAST` reads directly from a fixed-size C array pointer (`co_fastlocal`) inside the `PyFrameObject` using an integer index `O(1)` with zero hashing. `LOAD_GLOBAL` must perform a hash lookup on the module's `__dict__` dictionary, and if not found, perform a second hash lookup on the `builtins.__dict__` dictionary `O(2 * dict_lookup)`. This is why caching globals as local variables in hot loops is a classic optimization.

Trap

In Python 3.11+, `LOAD_GLOBAL` uses inline caching to speed up repeated lookups, but `LOAD_FAST` is still strictly faster.

Under The Hood — a multi-depth technical interview hub.

Third-party licenses Press ⌘ K to search.