Hi all
I am looking for a way to get the name of a Python function from inside the function.
I tried by the inspect module, callin the stack() function …
import inspect
def f1():
s = inspect.stack()
f1()
… but I get a “__main__” error.

… And obviously, the error message shows me all those function names. 
But I’d like to get them without making the script crash.
How can I do that ?
Thanks !
Hi @emilio, using the inspect
module you can try:
import inspect
def DoSomething():
print inspect.currentframe().f_code.co_name
if __name__ == "__main__":
DoSomething()
Without that module, you can access __name__
as a property of the function eg:
def DoSomething():
print DoSomething.__name__
if __name__ == "__main__":
DoSomething()
_
c.
1 Like
emilio
3
Hi Clement
Your code works, thanks !
I had tried
f = inspect.currentframe()
i = inspect.getframeinfo( f )
And that also gave me the same error.
But
inspect.currentframe().f_code.co_name
works fine ! 
Regards