I know sys.settrace() works very well if I do something like the following
def add(a,b):
return a+b
def my_tracer():
pass
sys.settrace(my_tracer)
add(1,2)
My question is: Can I do the same thing for a general form of code implementation? Say I have my code written into a file, or I have the full code loaded in a very long string that would normally be run by something like
exec(code_file)
Is there a way to still do something like the following?
sys.settrace(my_tracer)
exec(code_file)
If so, how can I get rid of all the redundant components and only focus on the code execution as if the code was implemented (like def add) in the first place?
Intended outcome:
sys.settrace(my_tracer)
exec(code_file)
working just like
sys.settrace(my_tracer)
add(1,2)
Qiyao Wei is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.