W3docs

How to get a function name as a string?

In Python, you can use the built-in function attribute __name__ to get the name of a function as a string.

In Python, you can use the built-in function attribute __name__ to get the name of a function as a string. Here is an example code snippet:

Print a function name as a string in Python

python— editable, runs on the server

This code defines a function called my_function and then prints the name of the function using the __name__ attribute. The output will be my_function.

You can also use str() or repr()

Print a function name as a string in Python using str and repr functions

python— editable, runs on the server

Note that str(my_function) and repr(my_function) will give the string representation of the function object which includes the module name if the function is defined in a module and the memory location of the function.