I am trying to convert a code written in MATLAB to python. In it a function called 'dbstack' is heavily used. This function return the name of the calling function, its file name and the line number in the file from which it's called. This is recursively done for all predecessor files as well which means this gives information about the file that called the calling function. Is there any such equivalent for Python?
The traceback module is probably what you're looking for. It doesn't have any one function that completely replaces dbstack, but it has functions which collectively give a similar experience.
import traceback
# print to stderr
traceback.print_stack()
# grab stack lines and manually manipulate them
for line in traceback.format_stack():
print(line)
You can also find similar information in the inspect module. Stack traces aren't its primary purpose, but they're accessible there.
import inspect
# Each frame is a Named Tuple
# FrameInfo(frame, filename, lineno, function, code_context, index)
for frame in inspect.stack():
print(frame)