Python: how to get the *full* name of the function I am in

Viewed 1878

Is there a way so that the following code:

import traceback

def log(message):
    print "%s: %s" %(traceback.extract_stack()[0:-1][-1][2], message)

def f1():
    log("hello")

class cls(object):
    def f1(self):
        log("hi there")

f1()
mycls = cls()
mycls.f1()

displays:

f1: hello
cls.f1: hi there

instead of:

f1: hello
f1: hi there

?

I tried to use module 'inspect' but was not successful...

Julien

EDIT:

The point here is for 'log' function to be able to retrieve its caller name on its own (using traceback, inspect, or any mean necessary).

I do not want to pass the class name, or anything else than 'message' to the 'log' function.

2 Answers
Related