微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐