Debugging Python

Harry George harry.g.george at boeing.com
Fri Jan 9 11:29:46 CET 2004


Peter Hansen <peter at engcorp.com> writes:

> Ashley Lloyd wrote:
> > 
> > What do people generally use to debug their Python programs? I haven't seen
> > anything out there that walks through the code, but perhaps I'm looking in
> > the wrong places?
> 
> When I need to, which is rare, I either use print statements (a very
> solid technology dating from, oh, last century sometime ;-), or the 
> Python debugger module: pdb.  
> 
> Actually, all I ever do with pdb is this:
> 
>    # this is just before the code that I think is failing
>    import pdb
>    pdb.set_trace()
> 
>    # code that I'm about to trace through goes here
> 
> 
> And about the only features of pdb I've had to use are "r", "n", and
> the ability to inspect arbitrary variables.
> 
> I'm not sure about others, but when I design and code using test-driven 
> development (TDD), I tend not to need to debug almost ever.  The need
> to debug is usually when you aren't sure where a typo, or logic error,
> or other mistake has actually occurred.  When using TDD, it's exceedingly
> rare that when a test fails, I don't know exactly where the problem is
> without having to resort to traditional (slow, tedious, annoying) debugging.
> 
> -Peter


Interesting.  A python newbie asked me the same thing yesterday and I
told him almost exactly what you said: If you are doing test-based
(whether or not it is with someojne else and thus Agile), you don't
get into those grim debug sessions that are part of life in C/C++
land.

Actually what I said was something like 

"If you don't make mistakes, you don't need a debugger.  The way to
avoid mistakes is to test everytime you edit a line or two of code.

"If you need to, use my homegrown debug statements (which print the
module and method/function name, the message, and then flush).  If you
really get stuck, there is a debugger, but typically you just need
debuggers to solve memory problems.  Python mistakes tend to be much
higher level, and pretty obvious once you see the data values."

-- 
harry.g.george at boeing.com
6-6M31 Knowledge Management
Phone: (425) 342-5601



More information about the Python-list mailing list