Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
3 views
in Python by (47.6k points)

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 the bar to know the bar's name? Or better yet, foo.bar's name?

#foo.py

def bar():

   print "my name is", __myname__ # <== how do I calculate this at runtime?

1 Answer

0 votes
by (106k points)
To determine the function name without using traceback within that function you can use the inspect.stack method below is the code for the same:-
import inspectdef foo():

  print(inspect.stack()[0][3])

Related questions

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...