Skip to content
Tutorials Center
My learning

Topics

Python Reference ยท Standard Library: Concurrency, Network and Tools

The dis module

Reference for Python's dis module: dis.dis(), get_instructions, Bytecode, opnames, code objects, and why bytecode differs between Python versions.

When Python runs your code, it first compiles it to bytecode, a compact list of instructions for the interpreter's virtual machine. The dis module turns that bytecode back into readable form, so you can see what a function really does.

It is a learning and debugging tool. Bytecode is a CPython implementation detail that changes between releases, so never write programs that depend on specific instructions.

Python
import dis

def add(a, b):
    return a + b

dis.dis(add)

Try it

Python Listing instructions

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Python Comparing two ways to write the same thing

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Syntax

Python
dis.dis(x=None, *, file=None, depth=None, show_caches=False, adaptive=False)
dis.get_instructions(x, *, first_line=None, show_caches=False, adaptive=False)
dis.Bytecode(x, *, first_line=None, current_offset=None, show_caches=False, adaptive=False)
dis.code_info(x)
dis.show_code(x, *, file=None)
dis.opname
Name Description
x A function, method, class, generator, code object, source string or bytes of raw code. With no argument, dis.dis() shows the last traceback.
file Where dis.dis() writes its output. Defaults to sys.stdout.
depth How many levels of nested code objects (inner functions, lambdas) to disassemble. None means all.
adaptive Show the specialized instructions that CPython 3.11+ may have swapped in after warm-up.

Return value: dis.dis() prints and returns None. get_instructions() returns an iterator of Instruction named tuples with fields such as opname, opcode, arg, argval, argrepr, offset and starts_line. Bytecode(x) is an iterable object with .dis() that returns the listing as a string. code_info() returns a string.

Raises: TypeError if x is an object that has no code.

Usage notes

  • Output is version-specific. Python 3.11 added specialized and inline-cache instructions, 3.12 and later renamed or merged several opcodes, and 3.13 and 3.14 changed more. Compare listings only from the same version.
  • Each function has a code object in fn.__code__. Useful attributes include co_varnames (local names), co_names (global and attribute names), co_consts (constants) and co_argcount.
  • Constant folding. The compiler evaluates simple constant expressions such as 60 * 60 * 24 ahead of time, so you will find 86400 among the constants.
  • Nested code is included. A function containing a lambda or inner def shows those as separate code objects; depth=1 limits the listing to the outer function.
  • dis.Bytecode(fn).dis() returns a string, handy when you want to compare or store the listing instead of printing it.
  • Reading a listing: each line has the source line number (on the first instruction of a line), the byte offset, the opcode name and, for many opcodes, the argument followed by its meaning in parentheses. Jump targets are marked with >>.
  • Do not use bytecode length as a speed measure. Fewer instructions can still be slower: a call into C such as sum() is one instruction that does a lot of work.
  • Not a security boundary. Reading bytecode is fine, but loading untrusted bytecode with marshal can execute arbitrary code.