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.



filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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__)

chevron_right


Output :

The name of function is : GFG


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.