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 function def GFG(): return "You just called for success !!" # printing function name # using function.func_name print("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 function def 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
Recommended Posts:
- Python | dir() function
- ord() function in Python
- sum() function in Python
- Python | oct() function
- id() function in Python
- Help function in Python
- Python | hex() function
- Python | cmp() function
- Python | int() function
- Python map() function
- Python | now() function
- Python | property() function
- Python reversed() function
- vars() function in Python
- Python | frexp() Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



