Debugging: How to learn from your mistakes

Presenter Notes

20 minute introduction to Python

... is not what this is.

There are lots of good resources already out there for learning the language:

Presenter Notes

Why teach debugging?

Learning from success is easy (and fun), but failure will happen.

If we don't teach beginners how to learn from failure, we fail to teach them how to learn.

Presenter Notes

3 steps to self-sufficiency

  1. Read and understand error messages
  2. Experiment in the interactive interpreter
  3. pdb, the Python debugger

Presenter Notes

Read and understand error messages

Presenter Notes

Don't fear the traceback

Example of a common traceback:

Traceback (most recent call last):
  File "sample2.py", line 9, in <module>
    random_square()
  File "sample2.py", line 6, in random_square
    pring('Number: {num}'.format(num=val))
NameError: global name 'pring' is not defined

This is your most valuable tool for finding out what went wrong.

The traceback contains the dying words of a crashed program.

Presenter Notes

Traceback breakdown

Traceback (most recent call last):

  File "sample2.py", line 9, in <module>
    random_square()

  File "sample2.py", line 6, in random_square
    pring('Number: {num}'.format(num=val))

NameError: global name 'pring' is not defined

Presenter Notes

The error message

NameError: global name 'pring' is not defined

This last line is most important

<Type of error>: <helpful error message>

Errors (e.g. a NameError) are Python objects! All are documented online.

http://docs.python.org/3/library/exceptions.html

NameError is very common, especially when starting out

Presenter Notes

(most recent call last)

The final 3 lines show what whent wrong and where:

  File "sample2.py", line 6, in random_square
    pring('Number: {num}'.format(num=val))

NameError: global name 'pring' is not defined

The previous lines show how you got there:

  File "sample2.py", line 9, in <module>
    random_square()

Presenter Notes

Experiment in the interactive interpreter

Presenter Notes

Try things out

Instead of writing your whole program top-to-bottom, build your program in the interpreter first.

>>>val = input('Enter a number: ')
Enter a number: 23
>>>val
'23'
>>>type(val)
<class 'str'>
>>>int(val)
23

Building bigger pieces out of small pieces is a kind of preemptive debugging

Presenter Notes

Even better: IPython and BPython

http://ipython.org

$ pip install ipython
$ ipython3

http://www.bpython-interpreter.org

$ pip install bpython
$ bpython

If you need to install pip, refer to Brian's pip instructions at http://edmontonpy.com/presentations/

Presenter Notes

pdb, the Python debugger

Presenter Notes

Ultimate debugging power

Interact with your program as it executes step-by-step.

Can also inspect a crashed program at the moment the error occurred.

http://docs.python.org/3.3/library/pdb.html

http://pymotw.com/2/pdb/index.html

  • for Python 2, but still relevant

Unfortunately for beginners this means learning yet another set of commands at the same time they're learning the language itself. Still, learning pdb is an incredibly useful skill.

Presenter Notes

3 ways to use pdb

  1. Prompt/terminal: python -m pdb <myfile.py>
  2. Post-mortem from the interpreter: import pdb; pdb.pm()
  3. In code: import pdb; pdb.set_trace()

I don't demo #3 or talk about it further because it's the least convenient

Presenter Notes

pdb from the terminal

python -m pdb <myfile.py>

Starts with execution paused at the first line of your file.

Can step through line by line, see values of variables, change variables, etc.

Presenter Notes

pdb from the interpreter

After a crash: pdm.pm() (post-mortem)

Returns to execution state when the exception occurred.

Unfortunately cannot resume code execution post-mortem; use breakpoints for that.

Presenter Notes

Commands used in pdb demo

  • list - show where you are in the code
  • step - advance one line / function call
  • cont - start running again (until breakpoint)
  • jump - jump straight to a line number

Presenter Notes

Summary

If you know how to debug your programs, you know how to learn from failure.

We do beginners a disservice if we fail to teach them how to be self-sufficient.

Presenter Notes