By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,587 Members | 1,665 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,587 IT Pros & Developers. It's quick & easy.

tracing function calls

P: n/a
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 ....

Jul 19 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
"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>

Jul 19 '05 #2

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

Fredrik Lundh wrote:
"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>


Jul 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.