Python Glossary Русский

all Python 3disfunction

dis.findlabels()

code information

Returns the list of offsets that jumps lead to in raw bytecode — the starts of loops and branches.

Syntax

dis.findlabels(code_bytes)

It expects the raw co_code bytes, not a code object: findlabels(f.__code__) raises TypeError, you need f.__code__.co_code. A straight-line function with no if or loop yields an empty list. The offsets themselves differ between releases, so rely on their presence rather than values; for analysis get_instructions() is handier — instructions carry a jump_target field (3.13+).

Examples

import dis
def straight(x):
    return x + 1
def loop(n):
    for i in range(n):
        pass
print(dis.findlabels(straight.__code__.co_code))  # → []
print(len(dis.findlabels(loop.__code__.co_code)) > 0)  # → True
try:
    dis.findlabels(loop.__code__)
except TypeError as e:
    print(e)  # → object of type 'code' has no len()

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy