Skip to content
Related Articles

Related Articles

Improve Article

Python | How to get function name ?

  • Last Updated : 22 May, 2019
Geek Week

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)
Output :
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__)
Output :
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




My Personal Notes arrow_drop_up
Recommended Articles
Page :