For now, I got two ways to fetch the name of the current function/method in python
Function based
>>> import inspect
>>> import sys
>>> def hello():
... print(inspect.stack()[0][3])
... print(sys._getframe( ).f_code.co_name)
...
>>> hello()
hello
hello
Class based
>>> class Hello(object):
... def hello(self):
... print(inspect.stack()[0][3])
... print(sys._getframe( ).f_code.co_name)
...
>>> obj = Hello()
>>> obj.hello()
hello
hello
Any other ways which are more easier and efficient than this?
inspect.currentframe().f_code.co_name. You will find also other methods there. They either use the function's identifier or change the way the function is called (e.g. using a decorator).