[英]Get the name of the current method/function in Python
现在,我有两种方法来获取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
还有其他比这更容易和有效的方法吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.