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

1 Answer

0 votes
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()'




Related questions

+1 vote
Feb 15 in Python
0 votes
Dec 22, 2019 in Python