all Python 3disfunction
dis.findlinestarts()
code information
A generator of (offset, line number) pairs: which bytecode instructions start each source line.
Syntax
dis.findlinestarts(code)
It takes a code object, not a function, and returns a generator — wrap it in list() to iterate twice. Line numbers are absolute, counted from the top of the file, and the first pair usually points at the def line (where the internal RESUME lives). Since 3.13 the number may be None for instructions without a source line, so guard any arithmetic on it.
Examples
# Python 3.11+
import dis
def f(x):
y = x * 2
return y
code = f.__code__
starts = list(dis.findlinestarts(code))
print([line - code.co_firstlineno for _, line in starts if line is not None]) # → [0, 1, 2]
See also
Open in the full glossarydocs.python.orgReport an inaccuracy