Python Dis Module (Disassembler)
The dis module supports the analysis of CPython bytecode by disassembling it. The CPython bytecode which this module takes as an input is defined in the file Include/opcode.h and used by the compiler and the interpreter.
Importing the Module
import dis
Disassembling Functions
The dis.dis() function disassembles a class, method, function, or code object.
import dis
def myfunc(alist):
return len(alist)
dis.dis(myfunc)
Output Explanation: The output typically consists of columns indicating:
- The line number in the source code.
- The address of the instruction.
- The opcode name.
- The argument (if any).
- The human-readable interpretation of the argument.
Inspecting Code Objects
You can inspect the code object details using dis.show_code().
import dis
def add(a, b):
return a + b
dis.show_code(add)
Command Line Usage
You can also use dis as a script from the command line.
python -m dis myscript.py