tracing function calls

S

Stormbringer

Greetings,

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 ....
 
F

Fredrik Lundh

Stormbringer said:
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>
 
S

Stormbringer

Thank you Fredrik !
With a little tweaking for the right indentation it should prove useful
:)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top