Dec 22, 2019 in Python
Q: How Do You Monitor The Code Flow Of A Program In Python?

1 Answer

Dec 22, 2019
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':


    # Next, inspect the frame data and print information.

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


def demo2():

    print 'in demo2()'

def demo1():

    print 'in demo1()'



Click here to read more about Python
Click here to read more about Insurance

Related questions

Jun 12 in Python
Dec 19, 2019 in Python
Aug 29 in Python