I want to create some kind of debug output for python and want to pass a function name to another function for output.
The only reasonable way I found was as follows:
def foobar(): print 'hello world' print foobar.__name__
is there something that does the same for the current function to improve copying and pasting this line?
as is the C-equivalent printf("%s", __func__).
printf("%s", __func__)
sys._getframe can do this:
import sys def foobar(): print sys._getframe().f_code.co_name
One thing you could try is to create a decorator that does this:
def traceme(func): def newfunc(*args, **kwargs): ret = func(*args, **kwargs) print func.__name__ return ret return newfunc @traceme def foobar(): print 'hello world'
, , ...
print sys._getframe().f_code.co_name
( ):
def function_name(): return sys._getframe().f_back.f_code.co_name
, , :
>>> def bar(): ... return function_name() ... >>> bar() 'bar'
( , , Krumelur, , , Python =)