Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Disassembler for Python bytecode
The dis module in Python's standard library provides functions for analyzing Python bytecode by disassembling it into human-readable form. This helps with code optimization and understanding how Python executes your code internally.
Basic Disassembly with dis()
The dis() function generates a disassembled representation of Python code objects like functions, methods, or classes ?
import dis
def hello():
print("hello world")
dis.dis(hello)
2 0 LOAD_GLOBAL 0 (print)
2 LOAD_CONST 1 ('hello world')
4 CALL_FUNCTION 1
6 POP_TOP
8 LOAD_CONST 0 (None)
10 RETURN_VALUE
Bytecode Analysis with Bytecode Class
The Bytecode class provides detailed analysis by returning an iterator of instruction objects ?
import dis
def hello():
print("hello world")
bytecode_obj = dis.Bytecode(hello)
for instruction in bytecode_obj:
print(instruction)
Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='print', argrepr='print', offset=0, starts_line=2, is_jump_target=False) Instruction(opname='LOAD_CONST', opcode=100, arg=1, argval='hello world', argrepr="'hello world'", offset=2, starts_line=None, is_jump_target=False) Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=4, starts_line=None, is_jump_target=False) Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=6, starts_line=None, is_jump_target=False) Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=8, starts_line=None, is_jump_target=False) Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=10, starts_line=None, is_jump_target=False)
Code Information Functions
code_info()
Returns detailed information about a code object as a formatted string ?
import dis
def hello():
print("hello world")
print(dis.code_info(hello))
Name: hello Filename: <stdin> Argument count: 0 Positional-only arguments: 0 Kw-only arguments: 0 Number of locals: 0 Stack size: 2 Flags: OPTIMIZED, NEWLOCALS, NOFREE Constants: 0: None 1: 'hello world' Names: 0: print
show_code()
Prints the same information as code_info() in a formatted way ?
import dis
def hello():
print("hello world")
dis.show_code(hello)
Name: hello Filename: <stdin> Argument count: 0 Positional-only arguments: 0 Kw-only arguments: 0 Number of locals: 0 Stack size: 2 Flags: OPTIMIZED, NEWLOCALS, NOFREE Constants: 0: None 1: 'hello world' Names: 0: print
Disassembling String Code
You can disassemble code from strings using compile() and disassemble() ?
import dis
code_string = '''
a = 5
b = 6
total = a + b
print("sum =", total)
'''
code_object = compile(code_string, 'example', 'exec')
dis.disassemble(code_object)
2 0 LOAD_CONST 0 (5)
2 STORE_NAME 0 (a)
3 4 LOAD_CONST 1 (6)
6 STORE_NAME 1 (b)
4 8 LOAD_NAME 0 (a)
10 LOAD_NAME 1 (b)
12 BINARY_ADD
14 STORE_NAME 2 (total)
5 16 LOAD_NAME 3 (print)
18 LOAD_CONST 2 ('sum =')
20 LOAD_NAME 2 (total)
22 CALL_FUNCTION 2
24 POP_TOP
26 LOAD_CONST 3 (None)
28 RETURN_VALUE
Instruction Iterator
The get_instructions() function returns an iterator over instruction objects ?
import dis
def simple_func():
x = 10
return x
instructions = dis.get_instructions(simple_func)
for instruction in instructions:
print(f"{instruction.opname}: {instruction.argval}")
LOAD_CONST: 10 STORE_FAST: x LOAD_FAST: x RETURN_VALUE: None
Instruction Object Fields
| Field | Description |
|---|---|
opname |
Human readable operation name |
opcode |
Numeric code for operation |
arg |
Numeric argument (if any) |
argval |
Resolved argument value |
offset |
Start index in bytecode sequence |
starts_line |
Source line number (if any) |
is_jump_target |
True if other code jumps here |
Conclusion
The dis module is essential for understanding Python's execution model and optimizing code performance. Use dis.dis() for quick analysis and Bytecode class for detailed instruction-level examination.
