say I have a module like so:
def print_hello(name):
" prints a greeting "
print("Hello, {}!".format(name.title()))
I want to set a sys.settrace on my module, so whenever a function from my module is called, it prints a block to stdout, like so
CALLED FUNCTION: say_hello()
Hello, Alex!
Predictably, the trace method will pick up ALL methods which are called, which results in this:
$ python3 trace_example.py
Called function: print_hello
Hello, Alex!
Called function: _remove
How can I tell if a method is in the current module?
(here is my trace function if you were wondering:)
import sys
def tracefunc(frame, event, args):
if event == 'call':
print("Called function: {}()".format(frame.f_code.co_name))
sys.settrace(tracefunc)