美文网首页
python 获取当前调用的 class 名和方法名

python 获取当前调用的 class 名和方法名

作者: 忘了呼吸的那只猫 | 来源:发表于2019-01-22 17:21 被阅读13次

直接上代码:

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

获取class名就是 xx.__class__.__name__ xx是class实例
获取调用方法名:sys._getframe().f_code.co_name这个就不解释了,不要忘了引用sys模块就行

相关文章

网友评论

      本文标题:python 获取当前调用的 class 名和方法名

      本文链接:https://www.haomeiwen.com/subject/hblrjqtx.html