You will encounter the TypeError: ‘NoneType’ object is not callable if you try to call an object with a None value like a function. Only functions respond to function calls.

In this tutorial, we will look at examples of code that raise the TypeError and how to solve it.


TypeError: ‘nonetype’ object is not callable

Calling a function means the Python interpreter executes the code inside the function. In Python, we can only call functions. We can call functions by specifying the name of the function we want to use followed by a set of parentheses, for example, function_name(). Let’s look at an example of a working function that returns a string.

# Declare function

def simple_function():

    print("Hello World!")

# Call function

simple_function()
Hello World!

We declare a function called simple_function in the code, which prints a string. We can then call the function, and the Python interpreter executes the code inside simple_function().

None, string, tuple, or dictionary types do not respond to a function call because they are not functions. If you try to call a None value, the Python interpreter will raise the TypeError: ‘nonetype’ object is not callable.

Let’s look at examples of raising the error and how to solve them.

Example #1: Printing Contents From a File

Let’s look at an example of a program that reads a text file containing the names of famous people and prints the names out line by line.

The text file is called celeb_names.txt and contains the following names:

Leonardo DiCaprio

Michael Jordan

Franz Kafka

Mahatma Gandhi

Albert Einstein

The program declares a function that reads the file with the celebrity names.

# Declare function

def names():
    
    with open("celeb_names.txt", "r") as f:
        
        celebs = f.readlines()
        
        return celebs

The function opens the celeb_names.txt file in read-mode and reads the file’s contents into the celebs variable. We will initialize a variable to assign the celebrity names.

# Initialize variable 

names = None

We can then call our get_names() function to get the celebrity names then use a for loop to print each name to the console.

# Call function

names = names()

# For loop to print out names

for name in names:

    print(name)

Let’s see what happens when we try to run the code:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
1 names = names()

TypeError: 'NoneType' object is not callable

Unfortunately, we raise the NoneType object is not callable. This error occurs because we declared both a function and a variable with the same name. We declared the function first then the variable declaration overrides the function declaration. Any successive lines of code referring to names will refer to the variable, not the function.

Solution

We can either rename the function or the variable to solve this error. Adding a descriptive verb to a function name is a good way to differentiate from a variable name. Let’s change the function name from names() to get_names().

# Declare function with updated name

def get_names():

    with open("celeb_names.txt", "r") as f:

        celebs = f.readlines()

        return celebs

# Initialize variable with None

names = None

# Call function and store values in names variable

names = get_names()

# Loop over all names and print out

for name in names:

    print(name)

Renaming the function name means we will not override it when declaring the names variable. We can now run the code and see what happens.

Leonardo DiCaprio

Michael Jordan

Franz Kafka

Mahatma Gandhi

Albert Einstein

The code successfully works and prints out all of the celebrity names.

Alternatively, we could remove the initialization, as it is not required in Python. Let’s look at the revised code:

# Declare function with updated name

def get_names():

    with open("celeb_names.txt", "r") as f:

        celebs = f.readlines()

        return celebs

# Call function and store values in names variable without initializing

names = get_names()

# Loop over all names and print out

for name in names:

    print(name)
Leonardo DiCaprio

Michael Jordan

Franz Kafka

Mahatma Gandhi

Albert Einstein

Example #2: Calling a Function Within a Function

Let’s look at an example of two functions; one function executes a print statement, and the second function repeats the call to the first function n times.

def hello_world():

    print("Hello World!")

def recursive(func, n): # Repeat func n times

    if n == 0:

        return
    
    else:
        
        func()
        
        recursive(func, n-1)

Let’s see what happens when we try to pass the call to the function hello_world() to the recursive() function.

recursive(hello_world(), 5)
TypeError                                 Traceback (most recent call last)
      1 recursive(hello_world(), 5)

      3         return
      4     else:
      5         func()
      6         recursive(func, n-1)
      7 

TypeError: 'NoneType' object is not callable

Solution

To solve this error, we need to pass the function object to the recursive() function, not the result of a call to hello_world(), which is None since hello_world() does not return anything. Let’s look at the corrected example:

recursive(hello_world, 5)

Let’s run the code to see the result:

Hello World!

Hello World!

Hello World!

Hello World!

Hello World!

Summary

Congratulations on reading to the end of this tutorial. To summarize, TypeError’ nonetype’ object is not callable occurs when you try to call a None type value as if it were a function. TypeErrors happen when you attempt to perform an illegal operation for a specific data type. To solve this error, keep variable and function names distinct. If you are calling a function within a function, make sure you pass the function object and not the result of the function if it returns None.

For further reading on not callable TypeErrors, go to the articles:

To learn more about Python, specifically for data science and machine learning, go to the online courses page on Python.

Have fun and happy researching!