Solve - TypeError: 'tuple' object is not callable in Python

avatar

Borislav Hadzhiev

Last updated: Apr 20, 2022

banner

Check out my new book

Solve - TypeError: 'tuple' object is not callable in Python #

The Python "TypeError: 'tuple' object is not callable" occurs when we try to call a tuple as if it were a function. To solve the error, make sure to use square brackets when accessing a tuple at a specific index, e.g. my_tuple[0].

typeerror tuple object is not callable

Here is one example of how the error occurs.

main.py
my_tuple = ('a', 'b', 'c') # ⛔️ TypeError: 'tuple' object is not callable my_tuple(0) # 👈️ uses parenthesis instead of square brackets

The issue in the code sample is that we used parenthesis instead of square brackets when accessing the tuple at a specific index.

main.py
my_tuple = ('a', 'b', 'c') print(my_tuple[0]) # 👉️ 'a' print(my_tuple[1]) # 👉️ 'b' print(my_tuple[2]) # 👉️ 'c'

Another thing to look out for is that you're not overriding the built-in tuple() function.

main.py
# 👇️ overriding built-in tuple() function tuple = ('a', 'b', 'c') # ⛔️ TypeError: 'tuple' object is not callable result = tuple(['d', 'e', 'f'])

To solve the error in this situation, rename your variable and restart your script.

main.py
# ✅ no longer overriding built-in tuple() function my_tuple = ('a', 'b', 'c') result = tuple(['d', 'e', 'f'])

Make sure you don't have two tuples next to one another without separating them with a comma.

main.py
# ⛔️ TypeError: 'tuple' object is not callable my_list = [('a', 'b') ('c', 'd')]

We have a missing comma between the two tuples in the list, so Python thinks we are trying to call the first tuple.

To solve the error, place a comma between the tuples.

main.py
my_list = [('a', 'b'), ('c', 'd')]

Another common cause of the error is simply trying to call a tuple as a function.

main.py
my_tuple = ('a', 'b', 'c') # ⛔️ TypeError: 'tuple' object is not callable my_tuple()
To solve the error, you either have to remove the parenthesis or figure out how the variable got assigned a tuple instead of a function or a class.

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

main.py
def example(): return 'hello world' example = ('a', 'b', 'c') # ⛔️ TypeError: 'tuple' object is not callable print(example())
The example variable shadows the function with the same name, so when we try to call the function, we actually end up calling the variable.

Renaming the variable or the function solves the error.

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, tasks): # 👇️ this attribute hides the method self.tasks = tasks # 👇️ same name as class variable def tasks(self): return self.tasks emp = Employee(('dev', 'test')) # ⛔️ TypeError: 'tuple' object is not callable print(emp.tasks())

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 rename the class method.

main.py
class Employee(): def __init__(self, tasks): self.tasks = tasks def get_tasks(self): return self.tasks emp = Employee(('dev', 'test')) print(emp.get_tasks()) # 👉️ ('dev', 'test')

In case you declared a tuple by mistake, tuples are constructed in multiple ways:

  • Using a pair of parenthesis () creates an empty tuple
  • Using a trailing comma - a, or (a,)
  • Separating items with commas - a, b or (a, b)
  • Using the tuple() constructor
I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.