Accessing func_name from inside a function

James Thiele jamesthiele.usenet at gmail.com
Sat Mar 25 19:00:19 CET 2006


I'd like to access the name of a function from inside the function. My
first idea didn't work.

>>> def foo():
...     print func_name
...
>>> foo()
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "<stdin>", line 2, in foo
NameError: global name 'func_name' is not defined

My second attempt works but looks ugly.

>>> def foo():
...     import inspect
...     print inspect.stack()[0][3]
...
>>> foo()
foo

Is there a standard way of getting the name of a function from inside
the function?




More information about the Python-list mailing list