2. Debugging
numbers = [1, 2, 3, 4, 10, -4, -7, 0]
def all_even(l):
even_numbers = []
for number in l:
if number / 2 == 0:
even_numbers.extend(number)
return even_numbers
all_even(numbers)
3. One option: all the print statements!
for number in l:
print "Number is: ", number
if number / 2 == 0:
print number, " is an even number!"
even_numbers.extend(number)
print "List now includes:"
print even_numbers
4. Enter Pdb!
Pdb is the built-in Python
debugger.
It helps you debug programs
interactively, allowing you to
step into functions, test
variables, and much more.
*not really
5. Using Pdb
Import pdb, then insert where you’d like to start
debugging:
numbers = [1, 2, 3, 4, 10, -4, -7, 0]
import pdb; pdb.set_trace()
def all_even(l):
even_numbers = []...
7. Pdb Commands
l (list) -- shows where you are in your script
n (next) -- steps to next line of execution (but not into functions!)
s (step) -- steps into next line of execution, including functions
r (return) -- steps to the end of currently executing function
c (continue) -- executes code
q (quit) -- exit the debugger
? (help) -- shows all commands
12. Recap
Pdb is awesome!
Use it when print statements are
cluttering your script.
Import: import pdb; pdb.set_trace()
Navigate with l, n, and s.
13. Resources
● Debugging in Python - Python Conquers the Universe
● Python Docs - the Python Debugger
● Tutorial: Debugging Python apps with Pdb
Hinweis der Redaktion
There are a number of bugs in this script! When you run it, you’ll get the following errors. Luckily for us, python errors are super helpful, but sometimes we want to find out what’s actually happening in our script, and when.
One method of debugging we’re all familiar with by now is printing. The problem here is that we soon end up with a LOT of print statements. We also might lose track of what variable we’re looking at, especially if the strings we’re printing aren’t super clear. This is more of an issue later on when we’re dealing with complex code.
Pdb is a bit like pressing pause in your program. It gives you the ability to stop and look at what’s happening under the hood -- check on variables, booleans, etc.
You import Pdb the same way you’d import any other module. Since I’m pretty confident my numbers list is fine, I’ll import pdb and set a trace just afterwards. However, theoretically I could set a trace wherever I’d like!
1st line = running my script, per usual
2nd line = shows what directory I’m in, and which line I’m currently evaluating
3rd line = line of code that is about to be evaluated
4th line = Pdb prompt; this is where you enter commands to continue, check on variables, etc
There are TONS of commands, so I’m just going to show a few basic ones.
Main difference between next and step = Step will take you INTO a function, next will simply run it. When you encounter a statement that doesn’t involve a function, they will do the same thing!
l = shows where you are in the script. In this case, we’re at line 5.
n = steps to next line of execution, which does NOT step into functions. In this case, since we were previously at a function call, n stepped over our function, which wasn’t what we wanted. Instead, we should use...
s = steps into next line of execution, including functions. As you can see, we are now on line 7, inside the function all_even(l)
At this point, s and n, will do the same thing!
Pdb allows you to check a given variable’s value -- this is possibly the most useful part! This is where pdb gets interactive!
In this example, we type in ‘number’ to check the value of number, which is 1.
Then, we check number / 2 (although you could theoretically check any equation here), and see that the result is 0. That’s not right!
We then step next (either n or s would work here, since we’re inside a function), and check that even_numbers is 0.