GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • w3resource
  • Front End
  • HTML
  • CSS
  • JavaScript
  • HTML5
  • Schema.org
  • php.js
  • Twitter Bootstrap
  • Responsive Web Design tutorial
  • Zurb Foundation 3 tutorials
  • Pure CSS
  • HTML5 Canvas
  • JavaScript Course

This view

  • Original page
  • New address

GeezerVison is showing www.w3resource.com — Python: Print the current call stack - w3resource

Python: Print the current call stack

Last update on May 17 2025 05:43:38 (UTC/GMT +8 hours)


Print Call Stack

Write a Python program to print the current call stack.

Sample Solution:

Python Code:

 # Import the 'traceback' module.
import traceback

# Print an empty line for formatting.
print()

# Define a function 'f1' that calls the 'abc' function.
def f1():
    return abc()

# Define the 'abc' function that prints the current stack using 'traceback.print_stack()'.
def abc():
    traceback.print_stack()

# Call the 'f1' function, which, in turn, calls the 'abc' function.
f1()

# Print an empty line for formatting.
print()

Sample Output:

File "7a070e70-25c4-11e7-bc90-3fdc1ec1c64d.py", line 5, in <module>                                         
    f1()                                                                                                      
  File "7a070e70-25c4-11e7-bc90-3fdc1ec1c64d.py", line 3, in f1                                               
    def f1():return abc()                                                                                     
  File "7a070e70-25c4-11e7-bc90-3fdc1ec1c64d.py", line 4, in abc                                              
    def abc():traceback.print_stack() 
	

Flowchart:

Flowchart: Print the current call stack.


For more Practice: Solve these Related Problems:

  • Write a Python program to capture and log the function call stack dynamically.
  • Write a Python program to print the call stack inside a recursive function.
  • Write a Python program to retrieve the function name that is currently executing.
  • Write a Python program to print the last function call before a program crashes.

Go to:

  • Python Basic Exercises Home ↩
  • Python Exercises Home ↩

Previous: Write a Python program to check if a string is numeric.
Next: Write a Python program to list the special variables used within the language.

Python Code Editor:

 

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.


  • Daily Coding Challenges & Projects
  • Weekly Trends and Language Statistics



You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.