0

How do I tell what happens when I call a function in python, as in, I have imported a module and want to call a function from it. I need to tell what functions this function calls and where they are located as it executes. Very similar to a traceback, but without an error. traceback.format_stack() won't work because I can't place it in the source code to run. Any ideas how to use this?

Example:

import amodule
#do something here?
amodule.some_method()
#or maybe here?

output:

File <fname1> in line 13, in <module>
    module.some_method()
File <fname2> in line 53, in <module>
    module2.some_other_method()

and so on

How can I best accomplish this?

Edit: The use I am thinking of for this is where there is a module composed of several files, which reference each other. If I want to edit a particular function, I need to know where and what to edit. I would rather not search through the source code to find the whole long chain of what happens where

cat40
  • 318
  • 1
  • 4
  • 13

1 Answers1

0

You could use Python Call Graph.

A call graph shows you which functions/methods are being called from where.

Alternatively, you could step through the code in a debugger.

But I would strongly suggest reading the code, and seeking to understand what it does.

I would rather not search through the source code to find the whole long chain of what happens where

There are no magical shortcuts. Reading the code can tell you how it works. The docstrings and other comments might even tell you why it works that way. The why can be even more important that the how.

Roland Smith
  • 42,427
  • 3
  • 64
  • 94