代码如下:
# coding=utf-8
import sys
class Hello():
def hello(self):
print('the name of method is ## {} ##'.format(sys._getframe().f_code.co_name))
print('the name of class is ## {} ##'.format(self.__class__.__name__))
if __name__ == "__main__":
h = Hello()
h.hello()
结果如下:
the name of method is ## hello ##
the name of class is ## Hello ##
获取class名就是 xx.__class__.__name__
其中xx是class实例
获取调用方法名:sys._getframe().f_code.co_name
这个就不解释了,不要忘了引用sys模块
就行