Till KTH:s startsida Till KTH:s startsida

Advanced assignment

Advanced Assignment

Completing this assignment will raise your grade one step.

A good development environment contains a debugger. The purpose of a debugger is to be able to get a in-depth view of a program as it executes in order to find errors. The naive, and common, way of debugging is to insert print instructions into the program that let you follow execution. However, this is not an efficient way of finding errors, especially in larger programs and/or large datasets. To enable efficient error-finding, you need to learn to use a debugger.

In a decent debugger you can expect to be able to

  • insert breakpoints: execution stops when arrived at a breakpoint and you can inspect the current state of the program,
  • step through the program line by line to see how the flow of the program execution,
  • show the value of variables using a simple print command, or
  • get a stack trace: this shows you the series of function calls that brought you to the current point in the program.

There are many more functionalities that are useful, and that you eventually are going to want to learn, but the above points are a good start.

Debugging in Python

There are several debuggers available for Python, and one easily accessible one is the pdb module which comes in the standard Python distribution. Read the documentation for pdb and start using it!

To present: Using one or more of the programs you have just written, you should be able to demonstrate that you know how to

  • insert a breakpoint at both a line in the program and at a function,
  • step through the program,
  • print values of variables, and
  • get a stack trace when arrived at a breakpoint.
  • A very nice way of solving the assignment is to take an old version of one of your programs and show how you find the problem with the above techniques.