Course map
Stage 1: Foundation
Stage 2: Intermediate
Stage 3: Advanced
Stage 4: DSA in Python
Stage 5: Specialization

Part 14 / Topic 8 of 11

Bytecode

Recap of Part 1: Python first compiles your source code into bytecode, a list of small, simple instructions, and then the interpreter runs them one by one. Think of a recipe turned into numbered cooking steps: "1. take amount, 2. take 1.18, 3. multiply, 4. serve".

source text  --compile()-->  code object (bytecode + names + constants)  --interpreter-->  result

compile and code objects

A code object holds the bytecode plus the names and constants it uses.

code = compile("total = 1760 * 2", "<bill>", "exec")
print(type(code).__name__)      # code
print(code.co_names)            # ('total',)
print(code.co_consts)           # (1760, None, 3520)   (1760 * 2 already worked out into 3520!)

scope = {}
exec(code, scope)               # run the code object
print(scope["total"])           # 3520

The compiler already did the multiplication: 3520 sits in co_consts alongside the original 1760, and the bytecode (check with dis.dis(code)) loads 3520 directly, never redoing 1760 * 2 at run time. The original 1760 stays in the tuple even though nothing loads it any more — an implementation detail of how the constant folder builds the table, not something to rely on.

Every function keeps its code object in func.__code__, for example func.__code__.co_varnames lists its local variable names.

dis: read the bytecode

The dis module (disassembler) prints the instructions in a readable form.

import dis

def add_gst(amount):
    return amount * 1.18

dis.dis(add_gst)

Output (CPython 3.13; names differ in other versions):

  3           RESUME                   0

  4           LOAD_FAST                0 (amount)
              LOAD_CONST               1 (1.18)
              BINARY_OP                5 (*)
              RETURN_VALUE
Column Meaning
3, 4 source line number
LOAD_FAST push the local variable amount onto the stack (a pile of values)
LOAD_CONST push the constant 1.18
BINARY_OP (*) pop two values, multiply, push the result
RETURN_VALUE pop the result and return it

What the interpreter loop does

The interpreter (in CPython, a big loop written in C) repeats: read the next instruction → do it → move on, working on a stack of values. Every instruction costs time, so fewer, simpler instructions usually mean faster code. That is why built-ins written in C (sum, sorted, set lookups) beat a Python loop: the loop runs many instructions per item, the built-in runs a single call.

The specialising adaptive interpreter (3.11+)

Since Python 3.11, the interpreter watches running code. After an instruction has run a few times with the same kinds of values, it swaps in a faster specialised version (PEP 659). You can see it with adaptive=True:

import dis

def add_gst(amount):
    return amount * 1.18

for _ in range(1_000):
    add_gst(100.0)              # warm up with floats

dis.dis(add_gst, adaptive=True)

Output (CPython 3.13):

  3           RESUME_CHECK             0

  4           LOAD_FAST                0 (amount)
              LOAD_CONST               1 (1.18)
              BINARY_OP_MULTIPLY_FLOAT 5 (*)
              RETURN_VALUE

BINARY_OP became BINARY_OP_MULTIPLY_FLOAT, a shortcut for "float times float". You get this speed-up for free; just keep the types in hot code consistent.

🔍 Going deeper: bytecode is cached in __pycache__/*.pyc files so imports skip compiling. Bytecode changes between versions, so never depend on it in real code.

Full program: examples/07_bytecode_and_timing.py.

Read more: the official dis documentation.

Done here? The practice set for Part 14 — 8 exercises with solutions — is on the part page.

Search the guide