功能的*全*名字有没有一种方法,使下面的代码:的Python:如何让我在
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()
显示:
f1: hello
cls.f1: hi there
代替:
f1: hello
f1: hi there
?
我试图用模块“检查”,但没有成功......
朱利安
编辑:
这里的要点是“日志”功能,能够检索它的主叫方 名称(使用追溯,检查或任何必要的意思)。
我不想将类名称或其他任何信息传递给'log'函数,而不是'message' 。
你可以使用['__qualname__'](https://www.python.org/dev/peps/pep-3155/)吗? – mgilson
https://stackoverflow.com/questions/10973362/python-logging-function-name-file-name-line-number-using-a-single-file –
@mgilson给出了问题中的python2.x语法I' d猜不? –