Python 获取当前函数的函数名

sys._getframe().f_code.co_name

# -*- coding:utf-8 -*-
import sys
 
 
class test_class():
    def hello(self):
        print(sys._getframe().f_code.co_name)
 
 
def hello_world():
    print('hello_world!')
    print(sys._getframe().f_code.co_name)
 
if __name__ == '__main__':
    ttt = test_class()
    ttt.hello()
 
    hello_world()

输出:

hello
hello_world!
hello_world
 
Process finished with exit code 0

转载自https://blog.csdn.net/lvhdbb/article/details/97923196

posted @ 2021-04-08 20:17  TesterBenja  阅读(139)  评论(0)    收藏  举报