On this page
  1. dis
  2. Import
  3. Overview
  4. Quick Start
  5. Implementation Walkthrough
  6. 1. Inspecting Bytecode Programmatically (dis.Bytecode)
  7. 2. Quick CLI Disassembly
  8. Gotchas & Boundaries
  9. Common Functions
  10. Classes & Objects
  11. Constants & Attributes

dis

Prev: difflib | Up: stdlib_module | Next: doctest
Rel: sys, sysconfig, builtins, warnings, dataclasses
Ref: docs.python.org — dis
Tags: #public #python3 #stdlib


Import

import dis

Overview

Disassembles CPython bytecode (dis.dis, dis.get_instructions, dis.Bytecode, dis.show_code) into human-readable opcodes, stack effects, and source line mappings. Essential for understanding Python performance differences, compiler optimizations, closure cells, and bytecode-level tools.

Quick Start

import dis

def add_one(x: int) -> int:
    return x + 1

ops = [instr.opname for instr in dis.get_instructions(add_one)]
assert "RETURN_VALUE" in ops

Implementation Walkthrough

1. Inspecting Bytecode Programmatically (dis.Bytecode)

import dis

bc = dis.Bytecode("x * 2 + 1")
lines = [f"{i.offset:3d} {i.opname} {i.argrepr}" for i in bc]
assert any("BINARY_OP" in line for line in lines)

2. Quick CLI Disassembly

python3 -m dis my_module.py

Gotchas & Boundaries

Common Functions

Classes & Objects

Constants & Attributes

Linked from

Related notes