all Python 3typesterm
types.FrameType
interpreter objects
The type of a stack frame: a running function call with its local variables, current line and a link to the calling frame, f_back.
Syntax
frame = inspect.currentframe()
You get a frame from inspect.currentframe() or from a traceback (tb.tb_frame). The trap is keeping it in a variable: a frame holds all local variables and the chain of callers, so they are not freed, and a frame that refers to itself forms a cycle that only the garbage collector clears. Once you have a frame, delete the reference (del frame) in finally. For logging the line and function name, logging or traceback is enough.
Examples
import inspect
import types
def who_called():
frame = inspect.currentframe()
try:
return frame.f_back.f_code.co_name
finally:
del frame
def report():
return who_called()
print(report()) # → report
print(isinstance(inspect.currentframe(), types.FrameType)) # → True
See also
Open in the full glossarydocs.python.orgReport an inaccuracy