1. 程式人生 > >python 獲取當前函式的 函式名 sys._getframe().f_code.co_name

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

--------------------- 本文來自 HeatDeath 的CSDN 部落格 ,全文地址請點選:https://blog.csdn.net/HeatDeath/article/details/72844487?utm_source=copy