Computer Science 15-112, Fall 2011
Class Notes: Debugging
# sample buggy code def digitFactors(n): # returns the number of digits of n that are factors of n count = 0 while (n > 0): digit = n % 10 n /= 10 if (n % digit == 0): # found another digit that divides n count += 1 return count # 6367 is prime, so digitFactors(6367) should equal 0 print digitFactors(6367) # prints 2 (uh oh)
def debug(): return __debug__ # set to False before you submit your hw! def db(*args): if debug(): for arg in args: print arg, print x = 0 for n in xrange(5): x += n db("n=",n,", x=",x) print x
h | help |
l | list source code near the current line |
w | where (print stack trace) |
a | arglist: print the arglist of the current function |
p expr | print the value of the expression |
pp expr | same as p only "pretty-printed" |
s | step into called functions |
n | next (step over called functions) |
r | return: run until this function returns |
c | continue (run again) |
!stmt | execute the stmt in the current context |
q | quit from the debugger |
carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem