Python Glossary Русский

all Python 3inspectfunction

inspect.getouterframes()

stack and frames

A list of FrameInfo records for the given frame and every frame outside it: the same as stack(), but starting from a frame you choose.

Syntax

inspect.getouterframes(frame, context=1) -> list[FrameInfo]

It takes a frame object, not a FrameInfo and not a function: pass inspect.currentframe() or fi.frame, otherwise you get AttributeError. It is handy to start at the caller, getouterframes(inspect.currentframe().f_back), so your own helper stays out of the report. Cost and leaks are the same as for stack(): context=1 reads the source of every frame, and a stored list keeps the frames alive.

Examples

import inspect
def log_callers():
    frame = inspect.currentframe().f_back  # начать с вызывающего
    try:
        return [fi.function for fi in inspect.getouterframes(frame, 0)]
    finally:
        del frame
def handler():
    return log_callers()
print(handler())  # → ['handler', '<module>']

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy