Welcome to akashITS (Akash IT Solutions), In this article, you will learn about: How to get a function name as a string in Python
How to get a function name as a string in Python
The first approach is by using the function property __name__. This property will return the name of the function as s string when the property is called. This is only present in Python 3.x version.
def sum(a, b): res = a + b return res # Get the function name as a string function_name = sum.__name__ print("Function name:", function_name) # Function name: sum
In this example, my_function.__name__ retrieves the name of the function as a string. When you run this code, it will print:This is a simple and reliable way to obtain the name of a function in Python.