Print current call stack from a method in Python code
问题 In Python, how can I print the current call stack from within a method (for debugging purposes). 回答1: Here's an example of getting the stack via the traceback module, and printing it: import traceback def f(): g() def g(): for line in traceback.format_stack(): print(line.strip()) f() # Prints: # File "so-stack.py", line 10, in <module> # f() # File "so-stack.py", line 4, in f # g() # File "so-stack.py", line 7, in g # for line in traceback.format_stack(): If you really only want to print the