PDB how to define a global inspection function?

Peter Otten __peter__ at web.de
Tue Feb 8 12:37:38 CET 2011


Charles Fox (Sheffield) wrote:

> Hi guys, I'm new to this group and have a question about debugging.
> I'm stepping through my code (using emacs pdbtrack and python-mode.el)
> and would like to isnpect objects as I go.  So I've defined a little
> object print function,
> 
> def p(obj):
>     print obj
>     print obj.__class__
>     d=dir(obj)
>     for a in d:
>         print a, "=", getattr(obj, a)
> 
> 
> however it only works if the function is imported by whatever module I
> am currently debugging.  I'd like it to be available globally so I can
> stop and inspect anything as I step through various modules (including
> external libraries).  Is there a way to put it in the global scope for
> pdb to use?   Also is there a way to automatically import it whenever
> pdb starts up (like a matlab startup file)?     (I'm not using ipython
> as it's not happy with pdbtrack in emacs, so am launching from emacs M-
> x pdb command).

For debugging purposes it's OK to put your function into the __builtin__ 
namespace:

>>> def p(): print 42
...
>>> import __builtin__
>>> __builtin__.p = p

Try it out:

>>> with open("tmp.py", "w") as f:
...     f.write("p()\n")
...
>>> import tmp
42




More information about the Python-list mailing list