In Python, without using the
traceback module, is there a way to determine a function’s name from within that function?
Say I have a module
foo with a function
bar. When executing
foo.bar(), is there a way for
bar to know
bar‘s name? Or better yet,
#foo.py def bar(): print "my name is", __myname__ # <== how do I calculate this at runtime?
Python doesn’t have a feature to access the function or its name within the function itself. It has been proposed but rejected. If you don’t want to play with the stack yourself, you should either use
bar.__name__ depending on context.
The given rejection notice is:
This PEP is rejected. It is not clear how it should be implemented or what the precise semantics should be in edge cases, and there aren’t enough important use cases given. response has been lukewarm at best.
Answered By – Rosh Oxymoron
Answer Checked By – Timothy Miller (BugsFixing Admin)