Skip to content
Related Articles

Related Articles

Python | How to get function name ?

View Discussion
Improve Article
Save Article
  • Last Updated : 15 Jul, 2022

One of the most prominent styles of coding is following the OOP paradigm. For this, nowadays, stress has been to write code with modularity, 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: Get Function Name in Python 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 for the Testing purpose and also for documentation at times. The drawback is that this works just for Python2. 

Python3




# 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: Get Function Name in Python 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 the above method had been depreciated in Python3

Python3




# 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

Method 3: Get Function Name in Python using __qualname__ attribute

The __qualname__ gives more complete information than __name__ and therefore can be more helpful in debugging. To extract the name from any object or class, you can also use its __qualname__ attribute. 

Python3




def Geekforgeeks():
    pass
 
class Geekforgeeks(object):
    def my_method(self):     
        pass
 
# "my_function"
print(Geekforgeeks.__qualname__)   
# "My_Class.my_method"
print(Geekforgeeks.my_method.__qualname__) 

Output :

Geekforgeeks
Geekforgeeks.my_method

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!