Python Glossary Русский

Python 3.4+disterm

dis.Bytecode

bytecode analysis

A wrapper around the bytecode of a function or code object: iterates over Instruction any number of times and returns the listing and info as strings.

Syntax

bc = dis.Bytecode(x, *, first_line=None, current_offset=None)

Unlike get_instructions() it can be iterated any number of times, and its .dis() method returns the listing as a string instead of printing it like dis.dis(). .info() is the same as dis.code_info(). Bytecode.from_traceback(tb) builds one with the failing instruction marked — a handy replacement for distb() when the text has to go into a log.

Examples

import dis
def f(x):
    return x + 1
bc = dis.Bytecode(f)
print(type(bc.dis()).__name__)  # → str
print(len(list(bc)) == len(list(bc)))  # → True
print(bc.info() == dis.code_info(f))  # → True

See also

Open in the full glossarydocs.python.orgReport an inaccuracy