On this page
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
- CPython bytecode opcodes change between minor Python versions (e.g., 3.11 vs 3.12 vs 3.14 adaptive/specialized instructions): Never hardcode opcode numbers across versions.
- Pass
adaptive=True/show_caches=Truetodis.dis()(Python 3.11+) to inspect CPython's inline specialization caches.
Common Functions
- dis.code_info() —
code_info(x)— Formatted details of methods, functions, or code. - dis.dis() —
dis(x=None, *, file=None, depth=None, show_caches=False, adaptive=False, show_offsets=False, show_positions=False)— Disassemble classes, methods, functions, and other compiled objects. - dis.disassemble() —
disassemble(co, lasti=-1, *, file=None, show_caches=False, adaptive=False, show_offsets=False, show_positions=False)— Disassemble a code object. - dis.disco() —
disco(co, lasti=-1, *, file=None, show_caches=False, adaptive=False, show_offsets=False, show_positions=False)— Disassemble a code object. - dis.distb() —
distb(tb=None, *, file=None, show_caches=False, adaptive=False, show_offsets=False, show_positions=False)— Disassemble a traceback (default: last traceback). - dis.findlabels() —
findlabels(code)— Detect all offsets in a byte code which are jump targets. - dis.findlinestarts() —
findlinestarts(code)— Find the offsets in a byte code which are start of lines in the source. - dis.get_instructions() —
get_instructions(x, *, first_line=None, show_caches=None, adaptive=False)— Iterator for the opcodes in methods, functions or code Generates a series of Instruction named tuples giving the details of each operations in the supplied code. - dis.show_code() —
show_code(co, *, file=None)— Print details of methods, functions, or code to file. - dis.stack_effect() —
stack_effect(opcode, oparg=None, /, *, jump=None)— Compute the stack effect of the opcode.
Classes & Objects
- dis.Bytecode — The bytecode operations of a piece of code Instantiate this with a function, method, other compiled object, string of code, or a code object (as returned by compile()). (Methods:
.dis(),.from_traceback(),.info()) - dis.Instruction — Details for a bytecode operation. (Methods:
.baseopcode(),.baseopname(),.cache_offset(),.count(),.end_offset(),.index())
Constants & Attributes
dis.EXTENDED_ARG(int) =69dis.HAVE_ARGUMENT(int) =43dis.cmp_op(tuple) =('<', '<=', '==', '!=', '>', '>=')dis.hasarg(list) =[128, 255, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55...dis.hascompare(list) =[56]dis.hasconst(list) =[82]dis.hasexc(list) =[263, 264, 265]dis.hasfree(list) =[62, 90, 97, 111]dis.hasjabs(list) =[]dis.hasjrel(list) =[68, 70, 75, 76, 77, 100, 101, 102, 103, 106, 237, 248, 2...dis.hasjump(list) =[68, 70, 75, 76, 77, 100, 101, 102, 103, 106, 237, 248, 2...dis.haslocal(list) =[63, 83, 84, 85, 86, 87, 88, 89, 112, 113, 114, 261, 266]dis.hasname(list) =[61, 64, 65, 72, 73, 80, 91, 92, 93, 96, 110, 115, 116, 249]dis.opmap(dict) ={'CACHE': 0, 'RESERVED': 17, 'RESUME': 128, 'INSTRUMENTED...dis.opname(list) =['CACHE', 'BINARY_SLICE', 'BUILD_TEMPLATE', 'BINARY_OP_IN...
Linked from
Related notes
- tuts/python3/stdlib_module/difflib Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/doctest Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/stdlib_module Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/builtins Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/dataclasses Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/dis/Bytecode Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/dis/code_info Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/dis/dis Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/dis/disassemble Linked here; shared tags: #stdlib