C ADVANCED MATERIAL
C.1 Command line debugging
Spyder simply presents a graphical interface to something called pdb
- the
Python Debugger. You can also use it from the command line - you just need to
know a few keyboard shortcuts.
We will not work through this section in class - it is for those who intend to take programming more seriously in the future.
You start the interactive Python debugger using the ipdb3
command in the
terminal. You just give it your script name, so: ipdb3 pop2_debug_script1.py
:

Starting ipdb3
You then have access to the same functionality, but from the keyboard only.
The relevant keys are:
- Step:
n
- Step into:
s
- Set breakpoint:
b
followed by the line number (e.g.b 5
to set a breakpoint on line 5) - Continue
c
- Quit:
q
- List code:
l
followed by line number (e.g.l 1
to show code around line 1) - Show current code:
ll
You can also type Python at the prompt, such as print(width)
.
The following terminal image shows the use of ipdb3
to examine the same problem
that we worked on before:

An example session from ipdb3
You are not expected to know how to use the command line Python debugger for the course.
C.2 Code Style
We have previously talked about the importance of making your code as clear for the reader as possible. This might mean writing clear comments which explain your thinking, naming your variables properly or trying to avoid over-complex lines of code.
Some of this is subjective and therefore requires human judgement. Other items, however, are more mechanical and these checks can be automated.
C.2.1 IDE ‘Linting’
Spyder has a built in style and simple error checker that you can turn on from the menu: This is a bit like the ‘Spell check’ in a word processor. It can’t tell if your code makes logical sense but it can at least tell if you are spelling things right and have formatted your ‘if’ blocks correctly. You can turn it on using the ‘Source’ menu at the top. There are lots of options for what to check. When you are learning it is nice to turn on as many as possible so that you are forced to write the nicest code possible right from the start.