Python: Print the current call stack

Python Basic: Exercise-96 with Solution

Write a Python program to print the current call stack.

Sample Solution:-

Python Code:

import traceback
def f1():return abc()
def abc():traceback.print_stack()

Sample Output:

File "7a070e70-25c4-11e7-bc90-3fdc1ec1c64d.py", line 5, in <module>                                         
  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: Print the current call stack.

Python Code Editor:


Have another way to solve this solution? Contribute your code (and comments) through Disqus.

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.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz

Python: Tips of the Day

List comprehension:

>>> m = [x ** 2 for x in range(5)]
>>> m
[0, 1, 4, 9, 16]