This content originally appeared on DEV Community and was authored by Muhammad Atif Iqbal
Understanding inspect.stack()
in Python with Examples
When you use Python’s inspect.stack()
, it gives you a list of frames representing the current call stack.
Each element is a 6-tuple:
-
frame (
frame_record[0]
) → The execution environment (locals, globals). -
filename (
frame_record[1]
) → The file name where the code lives. -
lineno (
frame_record[2]
) → The line number being executed. -
function (
frame_record[3]
) → The function name being executed. -
code_context (
frame_record[4]
) → A snippet of source code being run. -
index (
frame_record[5]
) → Which line of the code context is active.
Let’s break these with 5 different examples each.
1. Frame (frame_record[0]
)
This is the actual frame object: an environment where Python is executing.
You can use it to see local variables.
Examples
import inspect
def foo(x):
stack = inspect.stack()
frame = stack[0][0] # current frame
print(frame.f_locals) # show local variables
foo(10)
Output:
{'x': 10, 'stack': [...], 'frame': <frame object ...>}
Other uses:
- See which variables are in scope.
- Modify local variables dynamically (advanced).
- Access global variables.
- Debugging when you don’t know what’s available.
- Introspection tools (like Django debug toolbar).
2. Filename (frame_record[1]
)
This gives you the file name where the function is running.
Examples
import inspect
def bar():
stack = inspect.stack()
print(stack[0][1]) # filename
bar()
Output:
/home/user/my_script.py
Other uses:
- Logging errors with file location.
- Auto-documentation (e.g., Sphinx-like tools).
- Debugging when you forget which file a function lives in.
- Security: detect if code is executed from a wrong file.
- Cross-file function tracing.
3. Line Number (frame_record[2]
)
This tells you the line number inside the file.
Examples
import inspect
def baz():
stack = inspect.stack()
print(stack[0][2]) # line number
baz()
Output:
7
(or whatever line print
is on)
Other uses:
- Show where an error happened.
- Trace execution step-by-step.
- Build a debugger.
- Logging warnings with file + line.
- Unit test coverage tools.
4. Function (frame_record[3]
)
This gives you the function name currently executing.
Examples
import inspect
def alpha():
stack = inspect.stack()
print(stack[0][3]) # function name
alpha()
Output:
alpha
Other uses:
- Dynamic logging (
Entering function alpha
). - Profiling which function runs slow.
- Security: check only certain functions run.
- Frameworks (like Django middleware) detect caller.
- Debugging complex recursive functions.
5. Code Context (frame_record[4]
)
This is a list of lines of code currently running.
It’s usually 1 line.
Examples
import inspect
def beta():
stack = inspect.stack()
print(stack[0][4]) # code context
beta()
Output:
['print(stack[0][4]) # code context\n']
Other uses:
- Show exact line being run (like traceback).
- Pretty error printing.
- Static analyzers (linting tools).
- Debugger that shows current code.
- Learning tools that display executed code.
6. Index (frame_record[5]
)
This is the index of the line in the code_context
list.
Usually it’s 0
, but if multiple lines exist, it shows which one is running.
Examples
import inspect
def gamma():
stack = inspect.stack()
print(stack[0][5]) # index
gamma()
Output:
0
Other uses:
- Multi-line expressions debugging.
- Syntax highlighting for running line.
- Editors showing current cursor position.
- Teaching tools for step-by-step code.
- Debuggers aligning executed line.
Summary
-
frame
→ Execution environment (variables). -
filename
→ Which file is running. -
lineno
→ Which line number. -
function
→ Which function name. -
code_context
→ The actual code text. -
index
→ Position inside that code.
This content originally appeared on DEV Community and was authored by Muhammad Atif Iqbal