现在,我有两种方法可以在 python 中获取当前函数/方法的名称
基于函数
>>> import inspect
>>> import sys
>>> def hello():
... print(inspect.stack()[0][3])
... print(sys._getframe( ).f_code.co_name)
...
>>> hello()
hello
hello
基于类
>>> class Hello(object):
... def hello(self):
... print(inspect.stack()[0][3])
... print(sys._getframe( ).f_code.co_name)
...
>>> obj = Hello()
>>> obj.hello()
hello
hello
还有其他比这更简单有效的方法吗?