When we call the object having the value None, we might encounter TypeError: ‘nonetype’ object is not callable.
In this guide, we will deal with this error, why it occurs, and what it means.
Apart from that, we have example codes, and scenarios to help you understand the error and solve it quickly in your program.
When working with Python, Nonetype data type is used to store the None value.
Additionally, the keyword None is used to store the value none/null in a variable.
Now let’s understand why we get Typeerror: nonetype object is not callable.
What is Typeerror: nonetype object is not callable?
The Typeerror: nonetype object is not a callable error occurs if we call the None value as if it is a function.
To fix it, find where the values come from, hence correct the assignment, or get rid of the parenthesis.
Common Causes of ‘nonetype’ object is not callable
The common causes of this error include the following:
- Calling the None value inside the parenthesis as if it is a function.
- Having the same name for a variable and a function.
- Trying to call the function twice that returns twice.
- The calls method and class property have the same name.
- By mistake overriding the built-in function and setting it to None.
Example Scenario of nonetype object is not callable
Certainly, here is an example scenario of how this error occurs:
def get_name():
return None
name = get_name()
print(name())
In our example code, the get_name() function returns None, which means that the name variable will also have a value of None.
When we try to call name() in the print statement, we are essentially trying to call None as if it were a function.
Wherein it is not possible and will result in the “Typeerror: nonetype object is not callable” error message.
Now that you know how possibly it occurs, let’s fix it in the next section.
How to fix typeerror: ‘nonetype’ object is not callable
Here are the following solutions you can use depending on the cause of the error.
1. Rename the method of the Class
One of the causes of the error is having the same name of the class method and class property.
Take a look at this code snippet where it triggers the error:
class MyExample():
def __init__(self):
self.name = None
def name(self):
return self.name
ex1 = MyExample()
# TypeError: 'NoneType' object is not callable
print(ex1.name())
As you can see in the code Myexample have the same name on the attribute.
In which the attributes hide the method, so when the instance of the class is called in the method…
We trigger the error.
To fix the error make sure to change the name of the method of a class.
class MyExample():
def __init__(self):
self.name = None
def get_name(self):
return self.name
ex1 = MyExample()
print(ex1.get_name()) # returns None
Expected output:
None
2. Function and a variable with the same name
Another way to fix the error is to avoid having the same name on a function and variable.
Take a look at the example code below:
def example():
return 'I love itsourcecode'
example = None
# TypeError: 'NoneType' object is not callable
example()
Output:
Traceback (most recent call last): File "C:\Users\Windows\PycharmProjects\pythonProject\main.py", line 7, in <module> example() TypeError: 'NoneType' object is not callable
Having the same name in function and variable shadow the function set to None.
Wherein every time we call the function, we end up calling the variable.
Here is an example snippet code that fixed the error:
def Myexample():
return 'I love itsourcecode'
My_var = None
print(Myexample()) # I love itsourcecode
Expected Output:
I love itsourcecode
3. Remove the extra Parenthesis
Another cause of the error is having an extra parenthesis.
Examine the example error code below.
def example(x):
x()
def another_func():
print('I love itsourcecode')
# TypeError: 'NoneType' object is not callable
example(another_func())
The example function takes another function as an argument and calls it.
However, notice that we called the other function when passing it to example.
The function returns None, so the example function got called with a None value.
To fix the error we need to remove the extra parenthesis to call the example function.
def example(x):
x()
def another_func():
print('I love itsourcecode')
example(another_func) # I love itsourcecode
Expected output:
I love itsourcecode
Now the example function gets called with a function that invokes it and prints the message.
4. Check the return value of the method before calling it
Check the return value of the method before calling it. We can modify the method to return a lambda function that returns None.
Example error code:
class MyClass:
def my_method(self):
return None
my_obj = MyClass()
result = my_obj.my_method()()
print(result)
Output
TypeError: 'NoneType' object is not callable
To fix this we should modify the method to return lambda function that returns None.
Here is the snippet code:
class MyClass:
def my_method(self):
return lambda: None
my_obj = MyClass()
result = my_obj.my_method()()
print(result)
Output:
None
Conclusion
In conclusion, in fixing “TypeError: ‘NoneType’ object is not callable” error we take note of the following:
- Avoid having the same name on a function and a variable.
- Avoid having the same name on the class method and property.
- Do not call a None value with parentheses ().
- Do not override a built-in function and set it to None.
- Do not call a function that returns None twice.
If you are finding solutions to some errors you might encounter we also have Typeerror object of type float32 is not json serializable.
Thank you for reading!