Python Glossary Русский

Python 3.5+inspectterm

inspect.FrameInfo

stack and frames

A stack record returned by inspect.stack(), trace(), getouterframes() and getinnerframes(): frame, file, line, function and context.

Syntax

for info in inspect.stack(): info.function, info.lineno, info.filename

The frame field holds a live frame with all its variables: a stored list of FrameInfo keeps that memory alive, so delete it after use. Indexing (info[3]) still works for compatibility with the old tuple but is considered deprecated; write info.function. The typical task is finding the caller with inspect.stack()[1], which is costly because stack() reads source for every frame.

Examples

import inspect
def who_called_me():
    return inspect.stack()[1].function
def main():
    return who_called_me()
print(main())  # → main
import inspect
info = inspect.stack()[0]
print(type(info).__name__, info.function, info.lineno)  # → FrameInfo <module> 2
print(len(info), info[3] == info.function)  # → 6 True
del info

See also

Open in the full glossarydocs.python.orgReport an inaccuracy