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.
__qualname__
? – Sarettaframe
objects, I don't think you actually have a reference back to the function object -- Only it's name. – Sarettaf_code
but that's only the code object (a function object contains code + some other things) – Rickettsia