Python Glossary Русский

Python 3.4+disfunction

dis.get_instructions()

bytecode analysis

Returns an iterator of Instruction objects — bytecode as data you can filter and analyse in code.

Syntax

dis.get_instructions(x, *, first_line=None, adaptive=False)

It is a one-shot iterator: a second list() over it is empty; use dis.Bytecode to iterate again. Opcode names change between versions (LOAD_FAST turns into LOAD_FAST_BORROW in 3.14, BINARY_ADD became BINARY_OP in 3.11), so filtering by opname is reliable only for stable opcodes such as LOAD_GLOBAL. The show_caches parameter has had no effect since 3.13.

Examples

import dis
def size(s):
    return len(s)
names = [i.argval for i in dis.get_instructions(size) if i.opname == 'LOAD_GLOBAL']
print(names)  # → ['len']
import dis
def f(x):
    return x
it = dis.get_instructions(f)
print(len(list(it)) > 0)  # → True
print(len(list(it)))  # → 0

See also

Open in the full glossarydocs.python.orgReport an inaccuracy