Python Trace Tables

The ability to trace the values of variables during program execution is a great aid in ensuring your code is doing what it is supposed to do and in avoiding logic errors – those pesky bugs which don’t crash your program but give you unexpected results or even slip by you unnoticed only to come back and bite you later on.

In some Computer Science syllabuses this skill comes under the topic of Corrective maintenance. For example Cambridge Computer Science A Level has the following objectives relating to this topic:

  • Perform white-box testing by:
    • selecting suitable data
    • using a trace table
  • Identify any error(s) in the algorithm by using the completed trace table
  • Amend the algorithm if required

One way to keep track of values as a program executes is by using a trace table. The ability to create these is very helpful in the real-world development of software solutions as well as often being tested in Computer Science exam papers. While is is certainly worth being able to trace an algorithm manually, on paper or a whiteboard, is is also very useful to be able to do this programatically, so you can see the values of your variables at every step in a program’s execution for a given input.

There are several ways to achieve this with Python. We will look at two in this article.

Tracing Python Variables Using the sys Module

Here’s a very basic example showing how to trace the values of variables in a function call using the Python sys module.

A couple of things to note are:

  • If you want to trace code which is not in a function, you will need to “cheat” by putting it inside a function such as main() as in the example below. This is because the trace function given works by inspecting function call frames.

  • When you have called the function you wish to trace, you need to add sys.settrace(None) of you will get a whole lot of extra output which will likely not make much sense.


So what is going on here?

Well, we have told Python to use our user-defined function trace() to produce a trace of any function calls we make. So when well call main() the trace is created and output. If you look closely at the output for the given function, you can see a line-by-line listing of the event, the lineno (line number), and the f_locals – i.e. the local variables for the function currently being executed.

Pretty cool huh?

Let’s look at a more complex example.

Tracing the Fibonacci Function in Python


Tracing Recursive Function Calls in Python

Another situation where you might want to trace the execution of a program is to better understand what happens when a recursive function is called. You can read more about recursion here.

This can be achieved using the above method, as for example in the following example:


This is fine up to a point, but there is a way to get a clearer trace for recursive algorithms.

To use it, create a file with the following code in the same folder as where the recursive code you want to trace is:

Then you can use the trace helper function to get a nice, easy to read representation of the recursive calls, their arguments and their return values.

For example:

to get the super-handy output:

This article has shown you two ways you can get Python to give you valuable information about the your programs by tracing their execution. Try these methods out for yourself with your own programs if you need to develop your understanding of a particular algorithm or to debug a logic error.

Happy computing!

 Take your Python skills to the next level!

with our free email course on object oriented programming with Python