In Python, how do I get a function name as a string, without calling the function?
def my_function():
pass
print get_function_name_as_string(my_function
sys._getframe()
is not guaranteed to be available in all implementations of Python (see ref) ,you can use the traceback
module to do the same thing, eg.
import traceback
def who_am_i():
stack = traceback.extract_stack()
filename, codeline, funcName, text = stack[-2]
return funcName
A call to stack[-1]
will return the current process details.