tracing function calls

Discussion in 'Python' started by Stormbringer, Apr 23, 2005.

  1. Stormbringer

    Stormbringer Guest

    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 ....
     
    Stormbringer, Apr 23, 2005
    #1
    1. Advertisements

  2. "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>
     
    Fredrik Lundh, Apr 23, 2005
    #2
    1. Advertisements

  3. Stormbringer

    Stormbringer Guest

    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>
     
    Stormbringer, Apr 23, 2005
    #3
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Honne Gowda A
    Replies:
    2
    Views:
    960
    Karl Heinz Buchegger
    Oct 31, 2003
  2. Tracing Web service calls

    , May 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    10,598
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    May 29, 2006
  3. andy6
    Replies:
    2
    Views:
    850
    andy6 via DotNetMonster.com
    Jun 9, 2006
  4. Replies:
    2
    Views:
    1,020
    Bengt Richter
    Aug 1, 2005
  5. Bob
    Replies:
    5
    Views:
    319
Loading...

Share This Page