Python Glossary Русский

Python 3.4+disterm

dis.Instruction

bytecode analysis

A named tuple describing one bytecode instruction: opname, opcode, arg, argval, argrepr, offset, positions and more.

Syntax

Instruction(opname, opcode, arg, argval, argrepr, offset, ...)

You do not create it yourself — get_instructions() and Bytecode produce it. arg is the raw number, argval the resolved value (variable name, constant), argrepr its string for the listing. Fields have changed: positions with exact columns arrived in 3.11, and in 3.13 starts_line turned from a line number into a bool with the number moved to line_number — code reading starts_line as an int broke silently.

Examples

import dis
def f():
    return 'hello'
ins = [i for i in dis.get_instructions(f) if i.argval == 'hello'][0]
print(isinstance(ins, tuple))  # → True
print(ins._fields[:5])  # → ('opname', 'opcode', 'arg', 'argval', 'argrepr')
print(ins.argval, type(ins.arg).__name__)  # → hello int

See also

Open in the full glossarydocs.python.orgReport an inaccuracy