I know I'm gonna feel dumb in the Morning....

Peter Hansen peter at engcorp.com
Fri May 23 09:40:32 EDT 2003


"yaipa h." wrote:
> 
> > It will help if you learn to use the interactive prompt to
> > troubleshoot problems like this.
> 
> What you see below IS the interactive prompt from 'idle fork'.
> Should have checked the docs, but it just didn't occur to me
> that sort() would return none. Long day.

Actually, that's just the "traceback", I believe, although maybe you 
mean you *were* at the interactive prompt when you received it.  If
that's not the case, read on... :-)

What I meant by "interactive prompt" is that you could run Python 
from the command line, *not* executing your program immediately,
but instead importing the file and then manually executing various
functions by typing statements manually, one at a time.  When
you do that, if you got a traceback you would then start typing
commands and expressions to try to troubleshoot what went wrong.

For example, in this case, after seeing that traceback, you might
do the following (I've made a dummy dict called _fromDict for the
example):

> > Traceback (most recent call last):
> >   File "C:\jDev\cs-171\project\cs171\ctt.py", line 124, in -toplevel-
> >     for f in fLst:
> > TypeError: iteration over non-sequence

>>> fLst
>>> type(fLst)
<type 'None'>
>>> _fromDict.keys()
['b', 'c', 'a']
>>> _fromDict.keys().sort()
>>> [3, 2, 1].sort()
>>> a = [3, 2, 1]
>>> a.sort()
>>> a
[1, 2, 3]
>>> [].sort.__doc__
'L.sort([cmpfunc]) -- sort *IN PLACE*; if given, cmpfunc(x, y) -> -1, 0, 1'

At that point, resorting to checking the documentation for confirmation
would probably show what you needed.

Another approach I've taken quite successfully is to edit my file and
stick the following lines in just before or after the failure:

  import pdb
  pdb.set_trace()

When you do this you will get an interactive prompt, but from inside
the Python debugger!  Now (usually after hitting "r" and Enter, to get
back up to the context of the calling code) you can execute commands
interactively, including inspecting the *local* namespace of the function
you are in, or single-stepping through the code to see where it goes
wrong.  Very handy.

-Peter




More information about the Python-list mailing list