TypeError: 'NoneType' object is not callable in Python

avatar

Borislav Hadzhiev

Wed Apr 20 20222 min read

TypeError: 'NoneType' object is not callable in Python #

The Python "TypeError: 'NoneType' object is not callable" occurs when we try to call a None value as if it were a function. To solve the error, track down where the None value comes from and correct the assignment or remove the parenthesis.

typeerror nonetype object is not callable

Here is an example of how the error occurs.

main.py
example = None # ⛔️ TypeError: 'NoneType' object is not callable example()

You have to track down where the variable got assigned a None value and either correct the assignment to a function or class or remove the parenthesis.

Make sure you aren't calling a function twice.

main.py
# 👇️ this function returns None def do_math(a, b): print(a + b) # ⛔️ TypeError: 'NoneType' object is not callable do_math(10, 15)() # 👈️ remove extra set of parenthesis

The most common sources of None values are:

  1. Having a function that doesn't return anything (returns None implicitly).
  2. Explicitly setting a variable to None.
  3. Assigning a variable to the result of calling a built-in function that doesn't return anything.
  4. Having a function that only returns a value if certain condition is met.

Make sure you don't have a function and a variable that share the same name.

main.py
def example(): return 'hello world' example = None # ⛔️ TypeError: 'NoneType' object is not callable example()
The variable that we set to None shadows the function with the same name, so when we try to call the function, we end up calling the variable.

To solve the error, you have to either rename the function or rename the variable.

The error is also caused when we have a class method and a class property with the same name.

main.py
class Employee(): def __init__(self): self.name = None def name(self): return self.name emp1 = Employee() # ⛔️ TypeError: 'NoneType' object is not callable print(emp1.name())

The Employee class has a method and an attribute with the same name.

The attribute hides the method, so when we try to call the method on an instance of the class, we get the object is not callable error.

To solve the error, you have to make sure to rename the method in the class.

main.py
class Employee(): def __init__(self): self.name = None def get_name(self): return self.name emp1 = Employee() print(emp1.get_name()) # 👉️ None

Once you rename the method, you will be able to call it without any issues.

Use the search field on my Home Page to filter through my more than 1,000 articles.