Python | How to get function name ?
One of the most prominent styles of coding is following OOP paradigm. For this, nowadays, stress has been to write code with modularity, to increase debugging and create a more robust, reusable code. This all encouraged the use of different functions for different tasks, and hence we are bound to know certain hacks of functions. This article discusses how to print the name of a function. Let’s discuss certain ways in which this can be done.
Method #1 : Using function.func_name
By using a simple function property function, func_name, one can get the name of the function and hence can be quite handy in the Testing purpose and also for documentation at times. The drawback is that this works just for Python2.
# Python code to demonstrate# way to get function name# using function.func_name # initializing functiondef GFG(): return "You just called for success !!" # printing function name # using function.func_nameprint("The name of function is : " + GFG.func_name) |
The name of function is : GFG
Method #2 : Using function.__name__
This function can be used as an alternative to the above function and has been introduced in Python3 as the function mentioned in above method had been depreciated in Python3.
# Python code to demonstrate# way to get function name# using function.__name__ # initializing functiondef GFG(): return "You just called for success !!" # printing function name # using function.__name__print("The name of function is : " + GFG.__name__) |
The name of function is : GFG
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course


