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. Here is an example code snippet:

def my_function():
    pass

print(my_function.__name__) # Output: "my_function"

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()

def my_function():
    pass

print(str(my_function)) # Output: "<function my_function at 0x7f7c52f5a0d0>"
print(repr(my_function)) # Output: "<function my_function at 0x7f7c52f5a0d0>"

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.