How to get a function name as a string?

后端 未结 12 2230
后悔当初
后悔当初 2020-11-22 04:35

In Python, how do I get a function name as a string, without calling the function?

def my_function():
    pass

print get_function_name_as_string(my_function         


        
12条回答
  •  孤城傲影
    2020-11-22 04:57

    Try

    import sys
    fn_name = sys._getframe().f_code.co_name
    

    further reference https://www.oreilly.com/library/view/python-cookbook/0596001673/ch14s08.html

提交回复
热议问题