Python TypeError: Object is Not Callable. Why This Error?

Have you ever seen the TypeError object is not callable when running one of your Python programs? We will find out together why it occurs.

The TypeError object is not callable is raised by the Python interpreter when an object that is not callable gets called using parentheses. This can occur, for example, if by mistake you try to access elements of a list by using parentheses instead of square brackets.

I will show you some scenarios where this exception occurs and also what you have to do to fix this error.

Let’s find the error!

What Does Object is Not Callable Mean?

To understand what “object is not callable” means we first have understand what is a callable in Python.

As the word callable says, a callable object is an object that can be called. To verify if an object is callable you can use the callable() built-in function and pass an object to it. If this function returns True the object is callable, if it returns False the object is not callable.

callable(object)

Let’s test this function with few Python objects…

Lists are not callable

>>> numbers = [1, 2, 3]
>>> callable(numbers)
False

Tuples are not callable

>>> numbers = (1, 2, 3)
>>> callable(numbers)
False

Lambdas are callable

>>> callable(lambda x: x+1)
True

Functions are callable

>>> def calculate_sum(x, y):
...     return x+y
... 
>>> callable(calculate_sum)
True

A pattern is becoming obvious, functions are callable objects while data types are not. And this makes sense considering that we “call” functions in our code all the time.

What Does TypeError: ‘int’ object is not callable Mean?

In the same way we have done before, let’s verify if integers are callable by using the callable() built-in function.

>>> number = 10
>>> callable(number)
False

As expected integers are not callable 🙂

So, in what kind of scenario can this error occur with integers?

Create a class called Person. This class has a single integer attribute called age.

class Person:
    def __init__(self, age):
        self.age = age

Now, create an object of type Person:

john = Person(25)

Below you can see the only attribute of the object:

print(john.__dict__)
{'age': 25}

Let’s say we want to access the value of John’s age.

For some reason the class does not provide a getter so we try to access the age attribute.

>>> print(john.age())
Traceback (most recent call last):
  File "callable.py", line 6, in <module>
    print(john.age())
TypeError: 'int' object is not callable

The Python interpreter raises the TypeError exception object is not callable.

Can you see why?

That’s because we have tried to access the age attribute with parentheses.

The TypeError ‘int’ object is not callable occurs when in the code you try to access an integer by using parentheses. Parentheses can only be used with callable objects like functions.

What Does TypeError: ‘float’ object is not callable Mean?

The Python math library allows to retrieve the value of Pi by using the constant math.pi.

I want to write a simple if else statement that verifies if a number is smaller or bigger than Pi.

import math

number = float(input("Please insert a number: "))

if number < math.pi():
    print("The number is smaller than Pi")
else:
    print("The number is bigger than Pi")

Let’s execute the program:

Please insert a number: 4
Traceback (most recent call last):
  File "callable.py", line 12, in <module>
    if number < math.pi():
TypeError: 'float' object is not callable

Interesting, something in the if condition is causing the error ‘float’ object is not callable.

Why?!?

That’s because math.pi is a float and to access it we don’t need parentheses. Parentheses are only required for callable objects and float objects are not callable.

>>> callable(4.0)
False

The TypeError ‘float’ object is not callable is raised by the Python interpreter if you access a float number with parentheses. Parentheses can only be used with callable objects.

What is the Meaning of TypeError: ‘str’ object is not callable?

The Python sys module allows to get the version of your Python interpreter.

Let’s see how…

>>> import sys
>>> print(sys.version())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'str' object is not callable

No way, the object is not callable error again!

Why?

To understand why check the official Python documentation for sys.version.

Python sys version

That’s why!

We have added parentheses at the end of sys.version but this object is a string and a string is not callable.

>>> callable("Python")
False

The TypeError ‘str’ object is not callable occurs when you access a string by using parentheses. Parentheses are only applicable to callable objects like functions.

Error ‘list’ object is not callable when working with a List

Define the following list of cities:

>>> cities = ['Paris', 'Rome', 'Warsaw', 'New York']

Now access the first element in this list:

>>> print(cities(0))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'list' object is not callable

What happened?!?

By mistake I have used parentheses to access the first element of the list.

To access an element of a list the name of the list has to be followed by square brackets. Within square brackets you specify the index of the element to access.

So, the problem here is that instead of using square brackets I have used parentheses.

Let’s fix our code:

>>> print(cities[0])
Paris

Nice, it works fine now.

The TypeError ‘list’ object is not callable occurs when you access an item of a list by using parentheses. Parentheses are only applicable to callable objects like functions. To access elements in a list you have to use square brackets instead.

Error ‘list’ object is not callable with a List Comprehension

When working with list comprehensions you might have also seen the “object is not callable” error.

This is a potential scenario when this could happen.

I have created a list of lists variable called matrix and I want to double every number in the matrix.

>>> matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> [[2*row(index) for index in range(len(row))] for row in matrix]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 1, in <listcomp>
  File "<stdin>", line 1, in <listcomp>
TypeError: 'list' object is not callable

This error is more difficult to spot when working with list comprehensions as opposed as when working with lists.

That’s because a list comprehension is written on a single line and includes multiple parentheses and square brackets.

If you look at the code closely you will notice that the issue is caused by the fact that in row(index) we are using parentheses instead of square brackets.

This is the correct code:

>>> [[2*row[index] for index in range(len(row))] for row in matrix]
[[2, 4, 6], [8, 10, 12], [14, 16, 18]]

Conclusion

Now that we went through few scenarios in which the error object is not callable can occur you should be able to fix it quickly if it occurs in your programs.

I hope this article has helped you save some time! 🙂

Leave a Comment