Python Glossary Русский

all Python 3disfunction

dis.disassemble()

disassembly

Disassembles a single code object; the lasti parameter marks the current instruction with an arrow.

Syntax

dis.disassemble(code, lasti=-1, *, file=None)

Unlike dis.dis() it accepts only a code object: pass a function as f.__code__, otherwise you get AttributeError. Nested functions and lambdas are not disassembled — they stay constants, while dis.dis() recurses into them. lasti is the offset of the instruction that gets the --> arrow; that is how distb() points at the failing spot.

Examples

import dis, io
def f(x):
    return x * 2
buf = io.StringIO()
dis.disassemble(f.__code__, file=buf)
print(len(buf.getvalue()) > 0)  # → True
try:
    dis.disassemble(f)
except AttributeError as e:
    print(type(e).__name__)  # → AttributeError

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy