python 获取当前运行的 class 和 方法的名字

 

 

原文出处:python 获取当前运行的 class 和 方法的名字

 

# 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##

 

posted @ 2020-07-08 16:23  wangju003  阅读(1556)  评论(0)    收藏  举报