tracing function calls

Fredrik Lundh fredrik at pythonware.com
Sat Apr 23 05:06:52 EDT 2005


"Stormbringer" wrote:

> I've been wondering if there is a mechanism similar to trace/untrace
> found in lisp, for example call trace(function-name) and whenever this
> function is called it will show its parameters to stdout ....

def trace(func):
    def tracer(*args, **kwargs):
        print func.__name__, args, kwargs
        result = func(*args, **kwargs)
        print func.__name__, "return", result
        return result
    return tracer

def myfunc(a, b, c):
    return a + b + c

myfunc = trace(myfunc)

myfunc(1, 2, 3)

(tweak as necessary)

</F>




More information about the Python-list mailing list