其他分享
首页 > 其他分享> > 获取类名和函数名

获取类名和函数名

作者:互联网

#_*_ encoding: utf-8 _*_   @author: ty  hery   2019/7/17

import sys
import inspect
class testsqawd(object):
    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__':
    ttt = testsqawd()
    ttt.hello()

def my_name():
    print("01",sys._getframe().f_code.co_name)
    print("02",inspect.stack()[0][3])
my_name()

输出:

the name of method is ## hello##
the name of class is ## testsqawd ##
01 my_name
02 my_name

标签:__,函数,##,获取,print,my,class,类名,name
来源: https://www.cnblogs.com/heris/p/16492459.html