How to Get a Function Name as a String in Python?

In this python tutorial, we will learn how to get a function name as a string.

Table Of Contents

Get a function name as a string using __name__

Python3 version supports this method which is used to get the function name in string format. It also returns the module name.

Syntax:

 function.__name__

Here, the function is the function name. If you want to check the type of the returned function, then you can use the type() function. It returns the string class type.

Advertisements

Syntax:

type(function.__name__)

Example 1

In this example, we will create two functions and get the function names and their types using __name__ .

# Create a function named my_first_function.
def my_first_function():
    print ("This is my first function")

# Call the function
my_first_function()

# Get the function name as string
print ("Function name: ", my_first_function.__name__)

# Get the type
print ("Function type: ", type(my_first_function.__name__))

# Create a function named my_second_function.
def my_second_function():
    print ("This is my second function")

# Call the function
my_second_function()

# Get the function name as a string
print ("Function name: ", my_second_function.__name__)

# Get the type
print ("Function type: ", type(my_second_function.__name__))

Output:

This is my first function
Function name:  my_first_function
Function type:  <class 'str'>
This is my second function
Function name:  my_second_function
Function type:  <class 'str'>

You can see that the function name is returned and the type is str, which represents the string.

Example 2:

In this example, we imported two modules and get the module name and its type using name .

import math
import random

# Get the math module name as string
print ("Function name: ", math.__name__)

# Get the type of math 
print ("Function type: ", type(math.__name__))

# Get the random module name as string
print ("Function name: ", random.__name__)

# Get the type of random 
print ("Function type: ", type(random.__name__))

Output:

Function name:  math
Function type:  <class 'str'>
Function name:  random
Function type:  <class 'str'>

You can see that the module name is returned and the type is str, which represents the string.

Get a function name as a string using func_name

Python2 version supports this method which is used to get the function name in string format. It is deprecated in the python3 version.

Syntax:

function.func_name

Here, the function is the function name. If you want to check the type of the returned function, then you can use the type() function. It returns the class type.

Syntax:

type(function.func_name)

Example:

In this example, we will create two functions and get the function names and their types using func_name.

# Create a function named my_first_function.
def my_first_function():
    print ("This is my first function")

# Call the function
my_first_function()

# Get the function name as string
print ("Function name: ", my_first_function.func_name)

# Get the type
print ("Function type: ", type(my_first_function.func_name))


# Create a function named my_second_function.
def my_second_function():
    print ("This is my second function")

# Call the function
my_second_function()

# Get the function name as a string
print ("Function name: ", my_second_function.func_name)

# Get the type
print ("Function type: ", type(my_second_function.func_name))

Output:

This is my first function
('Function name: ', 'my_first_function')
('Function type: ', <type 'str'>)
This is my second function
('Function name: ', 'my_second_function')
('Function type: ', <type 'str'>)

You can see that the function name is returned and the type is str, which represents the string. This code will not work with python3, it will work with previous versions of Python.

Get a function name as a string using qualname

Python3 supports this method which is used to get the function name in string format. It also returns the names of the classes and methods.

Syntax:

 function.__qualname__

Here, the function is the function name. If you want to check the type of the returned function, then you can use the type() function. It returns the class type.

Syntax:

 type(function.__qualname__)

Example 1

In this example, we will create two functions and get the function names and their types using qualname .

# Create a function named my_first_function.
def my_first_function():
    print ("This is my first function")

# Call the function
my_first_function()

# Get the function name as string
print ("Function name: ", my_first_function.__qualname__ )

# Get the type
print ("Function type: ", type(my_first_function.__qualname__ ))

# Create a function named my_second_function.
def my_second_function():
    print ("This is my second function")

# Call the function
my_second_function()

# Get the function name as a string
print ("Function name: ", my_second_function.__qualname__ )

# Get the type
print ("Function type: ", type(my_second_function.__qualname__ ))

Output:

This is my first function
Function name:  my_first_function
Function type:  <class 'str'>
This is my second function
Function name:  my_second_function
Function type:  <class 'str'>

You can see that the function name is returned and the type is str, which represents the string.

Example 2:
In this example, we will create a class and get the module name and its type using name .

# Define a class with a method - hello
class My_class():
    def hello(self):     
        pass

# Get the class name as a string
print ("Class name: ", My_class.__qualname__ )

# Get the class type
print ("Class type: ", type(My_class.__qualname__ ))

# Get the method name as a string
print ("Method name: ", My_class.hello.__qualname__ )

# Get the method type
print ("Method type: ", type(My_class.hello.__qualname__ ))

Output:

Class name:  My_class
Class type:  <class 'str'>
Method name:  My_class.hello
Method type:  <class 'str'>

In the above code, we created a class named My_class, and a method-hello is created inside it. using qualname, we returned the function name as a string.

Summary

In this tutorial, we discussed three ways to return a function name as a string in python. The func_name does not work in the python 3 versions. If you want to get the class name and methods names as strings, you can use qualname .

Pandas Tutorials -Learn Data Analysis with Python

   

Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Join a LinkedIn Community of Python Developers

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top