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模块就行

相关文章

功能概要:(目前已实现功能)公共展示部分:1.网站首页展示...
大体上把Python中的数据类型分为如下几类: Number(数字) ...
开发之前第一步,就是构造整个的项目结构。这就好比作一幅画...
源码编译方式安装Apache首先下载Apache源码压缩包,地址为ht...
前面说完了此项目的创建及数据模型设计的过程。如果未看过,...
python中常用的写爬虫的库有urllib2、requests,对于大多数比...