Objects with the value None cannot be called. This is because None values are not associated with a function. If you try to call an object with the value None, you’ll encounter the error “TypeError: ‘nonetype’ object is not callable”.
In this guide, we discuss why this error is raised and what it means. We’ll walk through an example of this error to help you understand how you can solve it in your program.
TypeError: ‘nonetype’ object is not callable
When you call a function, the code inside the function is executed by the Python interpreter.
Only functions can be called. To call a function, you need to specify the name of a function, followed by a set of parentheses. Those parenthesis can optionally contain arguments that are passed through to a function.
Consider the following code:
def show_languages(): print("Python, Java, C, C++") show_languages()
First, declare a function called “show_languages”.
On the last line of our code, we call our function. This executes all the code in the block where we declare the “show_languages” function.
Similar to how you cannot call a string, a tuple, or a dictionary, you cannot call a None value. These data types do not respond to a function call because they are not functions. The result of calling a None value is always “TypeError: ‘nonetype’ object is not callable”.
An Example Scenario
Let’s build a program that reads a file with a leaderboard for a poker tournament and prints out each player’s position on the leaderboard to the console.
We have a file called poker.txt which contains the following:
1: Greg Daniels 2: Lucy Scott 3: Hardy Graham 4: Jenny Carlton 5: Hunter Patterson
We start by writing a function that reads the file with leaderboard information:
def tournament_results(): with open("poker.txt", "r") as file: tournament = file.readlines() return tournament
This function opens the “poker.txt” file in read mode and reads its contents into a variable called “tournament”. We return this variable to the main program.
Until we call our function, “tournament_results” will not have a value. So, we’re going to initialize a variable that stores our tournament scores:
tournament_results = None
Now that we’ve initialized this variable, we can move on to the next part of our program. We call our tournament_results()
function to get the tournament data. We then use a for loop to print each value to the console:
tournament_results = tournament_results() for t in tournament_results: print(t)
Our for loop prints each line from our file to the console. Let’s run our code and see what happens:
Traceback (most recent call last): File "main.py", line 8, in <module> tournament_results = tournament_results() TypeError: 'NoneType' object is not callable
Our code returns an error message.
The Solution
In our code, we have declared two values with the name “tournament_results”: a function and a variable. We first declare our function and then we declare our variable.
When we declare the “tournament_results” variable, we override the function. This means that whenever we reference “tournament_results”, our program will refer to the variable.
This causes a problem when we try to call our function:
tournament_results = tournament_results()
To solve this error, we should rename the variable “tournament_results” to something else:
final_results = None final_results = tournament_results() for f in final_results: print(f)
We have renamed the “tournament_results” variable to “final_results”. Let’s see whether this solves the error we experienced:
1: Greg Daniels 2: Lucy Scott 3: Hardy Graham 4: Jenny Carlton 5: Hunter Patterson
Our code successfully prints out all of the text in our file. This is because we’re no longer overriding the “tournament_results” with the value None.
Conclusion
“TypeError: ‘nonetype’ object is not callable” occurs when you try to call a None value as if it were a function.
To solve it, make sure that you do not override the names of any functions with a None value. Now you have the knowledge you need to fix this error like an expert!
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.