How to get a function name as a string?

后端 未结 12 2234
后悔当初
后悔当初 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:53

    my_function.func_name
    

    There are also other fun properties of functions. Type dir(func_name) to list them. func_name.func_code.co_code is the compiled function, stored as a string.

    import dis
    dis.dis(my_function)
    

    will display the code in almost human readable format. :)

提交回复
热议问题