Python 3.5+tracebackfunction
traceback.walk_stack()
frames and stack
A generator of (frame, line number) pairs from frame f outwards along f_back — the raw material for StackSummary.extract().
Syntax
for frame, lineno in traceback.walk_stack(f): ...
It yields live frame objects with f_locals and f_globals, not snapshots: you can read the callers' variables, but they stay in memory as long as you hold the frames. The order is the reverse of a traceback: the current frame first, the module last. Before Python 3.14 walk_stack(None) located its starting frame only on the first iteration, so list(walk_stack(None)) inside a function failed with AttributeError; pass the frame explicitly — sys._getframe().
Examples
import sys, traceback
def inner():
return [f.f_code.co_name for f, line in traceback.walk_stack(sys._getframe())]
def outer():
return inner()
print(outer()) # → ['inner', 'outer', '<module>']
See also
Open in the full glossarydocs.python.orgReport an inaccuracy