17.10 dis -- Disassembler for Python byte code
Keywords: lib

# 17.10 `dis` -- Disassembler for Python byte code

The `dis` module supports the analysis of Python byte code by disassembling it. Since there is no Python assembler, this module defines the Python assembly language. The Python byte code which this module takes as an input is defined in the file Include/opcode.h and used by the compiler and the interpreter.

Example: Given the function `myfunc`:

      def myfunc(alist):
          return len(alist)

the following command can be used to get the disassembly of `myfunc()`:

      >>> dis.dis(myfunc)
                0 SET_LINENO          1

                3 SET_LINENO          2
                6 LOAD_GLOBAL         0 (len)
                9 LOAD_FAST           0 (alist)
               12 CALL_FUNCTION       1
               15 RETURN_VALUE
               16 LOAD_CONST          0 (None)
               19 RETURN_VALUE

The `dis` module defines the following functions and constants:

`dis` ([bytesource])
  Disassemble the bytesource object. bytesource can denote either a class, a method, a function, or a code object. For a class, it disassembles all methods. For a single code sequence, it prints one line per byte code instruction. If no object is provided, it disassembles the last traceback.

`distb` ([tb])
  Disassembles the top-of-stack function of a traceback, using the last traceback if none was passed. The instruction causing the exception is indicated.

`disassemble` (code[, lasti])
  Disassembles a code object, indicating the last instruction if lasti was provided. The output is divided in the following columns:

  1. the current instruction, indicated as "`->`",
  2. a labelled instruction, indicated with "`»`",
  3. the address of the instruction,
  4. the operation code name,
  5. operation parameters, and
  6. interpretation of the parameters in parentheses.

  The parameter interpretation recognizes local and global variable names, constant values, branch targets, and compare operators.

`disco` (code[, lasti])
  A synonym for disassemble. It is more convenient to type, and kept for compatibility with earlier Python releases.

`opname`
  Sequence of operation names, indexable using the byte code.

`cmp_op`
  Sequence of all compare operation names.

`hasconst`
  Sequence of byte codes that have a constant parameter.

`hasname`
  Sequence of byte codes that access an attribute by name.

`hasjrel`
  Sequence of byte codes that have a relative jump target.

`hasjabs`
  Sequence of byte codes that have an absolute jump target.

`haslocal`
  Sequence of byte codes that access a local variable.

`hascompare`
  Sequence of byte codes of boolean operations.

Subsections

- 17.10.1 Python Byte Code Instructions