Python | How do I get the name of a function?

Python Methods and Functions

Method # 1: Using function.func_name

Using a simple function property of a function, func_name, one can get the name of a function and therefore can sometimes be very handy for testing purposes as well as for documentation. The downside is that this only works for Python2.

# Python code for demonstration
# a way to get the function name
# using function.func_name

 
# initializing function

def GFG () :

return "You just called for success !! "

  
# print 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 was introduced in Python3 since the function mentioned in the above method is deprecated in Python3.

# Python code for demonstration
# a way to get the function name
# using the function .__ name__

  
# initializing function

def GFG ():

return "You just called for success !!"

 
# the name of the print function
# using the function .__ name__

print ( "The name of function is:" + GFG .__ name__)

Output:

 The name of function is: GFG 




Get Solution for free from DataCamp guru