Dynamic function calling

Alex Martelli aleaxit at yahoo.com
Fri Sep 1 21:55:32 CEST 2000

"Olaf Meyer" <olaf.meyer at nokia.com> wrote in message
news:39AFF1FB.2A51DB10 at nokia.com...
> Thanks to all of you for the suggestions! I do have anoter small question:
> Is it possible to determine the name of the currently executing function?

Sure -- piece o' cake...:

def caller(n=1):
    from traceback import extract_stack
    stack = extract_stack()
    return stack[-n-2][2]

# Example usage:

def a():
    print "this is", caller(0)

def b():
    print "and this is",caller(0),
    print "called by",caller()

Expected output by calling a():

this is a
and this is b called by a


More information about the Python-list mailing list