0

Possible Duplicate:
How do I get the name of a function or method from within a Python function or method?

The below code function return it's name. It works but I still have to specify the 'test.' in front of of the __name__. Is there any general way to refer the __name__ ?

def test():
   print test.__name__
Community
  • 1
  • 1
prgbenz
  • 1,129
  • 4
  • 13
  • 27

2 Answers2

2

If I understand correctly - Inspect is what you are looking for.

import inspect
def test():
    print inspect.stack()[0][3]
Tim
  • 5,732
  • 2
  • 27
  • 35
0

There is no way to access the function name in a python function without using a backtrace.

In [1]: import inspect

In [2]: def test():
   ...:     print inspect.stack()[0][3]
   ...:

In [3]: test()
test

So, what you want to use is inspect.stack()[0][3] or, if you want to move it into a separate function, inspect.stack()[1][3]

(from How do I get the name of a function or method from within a Python function or method?)

Community
  • 1
  • 1
ThiefMaster
  • 310,957
  • 84
  • 592
  • 636