views:

58

answers:

2

The question is, how can I configure the Python debugger to show me in the console what functions are being called?

In order not to see everything flash by, a delay between the function calls would be needed.

+1  A: 

You can use the alternative pydb debugger. You can invoke it with pydb --fntrace --batch <scriptname> to get a function trace.

As for the "flashing-by", use the usual tools like Ctrl-S/Ctrl-Q on an ANSI terminal, or redirect to a file.

ThomasH
I think this debugger can do the job that pdb can not! I found a nice tutorial on it at showmedo.com/videotutorials/… . See the "set linetrace delay 0.35" functionality in the middle of it. Thanks
cmdev
+2  A: 

If you want to monitor when a few particular functions are being called, you could use this decorator:

import functools
def trace(f):
    @functools.wraps(f)
    def wrapper(*arg,**kw):
        '''This decorator shows how the function was called'''
        arg_str=','.join(['%r'%a for a in arg]+['%s=%s'%(key,kw[key]) for key in kw])
        print "%s(%s)" % (f.__name__, arg_str)
        return f(*arg, **kw)
    return wrapper

You would use it like this:

@trace          # <--- decorator your functions with the @trace decorator
def foo(x,y):
    # do stuff

When you run your program, every time foo(x,y) is called, you'll see the function call with the value of its arguments in the console:

foo(y=(0, 1, 2),x=(0, 0, 0))
unutbu
Interesting functionality. In what versions of Python can you do this?
cmdev
Decorators were introduced in 2.4, and functools was introduced in 2.5. There is an alternative way to write the trace decorator which does not rely on functools: See http://pypi.python.org/pypi/decorator#a-trace-decorator
unutbu