0 votes
in Python by
How Do You Monitor The Code Flow Of A Program In Python?

1 Answer

0 votes
by
In Python, we can use the sys module’s settrace() method to setup trace hooks and monitor the functions inside a program.

You need to define a trace callback method and pass it to the settrace() function. The callback should specify three arguments as shown below.

import sys

def trace_calls(frame, event, arg):

    # The 'call' event occurs before a function gets executed.

    if event != 'call':

        return

    # Next, inspect the frame data and print information.

    print 'Function name=%s, line num=%s' % (frame.f_code.co_name, frame.f_lineno)

    return

def demo2():

    print 'in demo2()'

def demo1():

    print 'in demo1()'

    demo2()

sys.settrace(trace_calls)

demo1()

Related questions

0 votes
asked Sep 29, 2021 in Python by john ganales
0 votes
asked Sep 29, 2021 in Python by john ganales
...