Python Glossary Русский

all Python 3tracebackfunction

traceback.extract_stack()

frames and stack

A snapshot of the current call stack as a StackSummary: where the program is now and who called it — no exception needed.

Syntax

traceback.extract_stack(f=None, limit=None)

Unlike extract_tb, which needs an exception's traceback, extract_stack looks at the live stack right here: this is how you find out who called a function. Frames go from the outermost to the current one; the last item is the line calling extract_stack itself, so the caller is item [-2]. limit=N keeps the N frames closest to the current point, not the first N.

Examples

import traceback
def who_called():
    return traceback.extract_stack(limit=2)[0].name
def report():
    return who_called()
print(report())  # → report

See also

Open in the full glossarydocs.python.orgReport an inaccuracy