TypeError: 'tuple' object is not callable in Python [Fixed]

avatar
Borislav Hadzhiev

Last updated: Apr 8, 2024
5 min

banner

# 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 parentheses instead of square brackets

The issue is that we used parentheses instead of square brackets when accessing the tuple at a specific index.

# Use square brackets to access a tuple at an index

Use square brackets when accessing elements of a tuple.

main.py
my_tuple = ('a', 'b', 'c', 'd') print(my_tuple[0]) # ๐Ÿ‘‰๏ธ 'a' print(my_tuple[1]) # ๐Ÿ‘‰๏ธ 'b' print(my_tuple[2]) # ๐Ÿ‘‰๏ธ 'c'

use square brackets to access tuple at index

Python indexes are zero-based, so the first item in a tuple has an index of 0, and the last item has an index of -1 or len(my_tuple) - 1.

You should also use square brackets if you need to get a slice of a tuple.

main.py
my_tuple = ('a', 'b', 'c', 'd') print(my_tuple[0:2]) # ๐Ÿ‘‰๏ธ ('a', 'b') print(my_tuple[2:4]) # ๐Ÿ‘‰๏ธ ('c', 'd')

use square brackets to get slice of tuple

The syntax for tuple slicing is my_tuple[start:stop:step].

The start index is inclusive and the stop index is exclusive (up to, but not including).

If the start index is omitted, it is considered to be 0, if the stop index is omitted, the slice goes to the end of the tuple.

# Common causes of the error

The error occurs for multiple reasons:

  • Trying to access a tuple with parentheses () instead of square brackets [].
  • Having a function and a variable with the same name.
  • Overriding a built-in function by mistake and setting it to a tuple.
  • Having a class method and a class property with the same name.
  • Calling a function that returns a tuple twice.

# Make sure you aren't overriding the tuple() function

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

main.py
# ๐Ÿ‘‡๏ธ Overriding the 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 the built-in tuple() function my_tuple = ('a', 'b', 'c') result = tuple(['d', 'e', 'f'])

# Forgetting to place a comma between two tuples in a list

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')]

# Trying to call a tuple as a function causes the error

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 parentheses or figure out how the variable got assigned a tuple instead of a function or a class.

main.py
my_tuple = ('a', 'b', 'c') print(my_tuple) # ๐Ÿ‘‰๏ธ ('a', 'b', 'c')

Once we removed the parentheses, we were able to print the tuple without any issues.

If the variable got assigned a tuple by mistake, you have to correct the assignment.

# Having a function and a variable with the same name

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

main.py
def example(): return 'bobbyhadz.com' 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.

main.py
def example(): return 'bobbyhadz.com' my_tuple = ('a', 'b', 'c') print(example()) # ๐Ÿ‘‰๏ธ bobbyhadz.com

Now the tuple and the function have different names that no longer clash and the issue is resolved.

# Having a class method and a class property with the same name

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')

The property and the method now have different names, so the issue is resolved.

# Calling a function that returns a tuple twice

The error also occurs when you call a function that returns a tuple twice.

main.py
def my_func(): return ('bobby', 'hadz', 'com') # โ›”๏ธ TypeError: 'tuple' object is not callable print(my_func()())

The first set of parentheses calls the function and the function returns a tuple.

The second set of parentheses tries to call the tuple as a function and causes the error.

Remove the second set of parentheses to resolve the issue.

main.py
def my_func(): return ('bobby', 'hadz', 'com') print(my_func()) # ๐Ÿ‘‰๏ธ ('bobby', 'hadz', 'com')

# How tuples are constructed in Python

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

  • Using a pair of parentheses () creates an empty tuple
  • Using a trailing comma - a, or (a,)
  • Separating items with commas - a, b or (a, b)
  • Using the tuple() constructor

# Conclusion

To solve the "TypeError: 'tuple' object is not callable" error, make sure:

  • You aren't trying to access a tuple with parentheses () instead of square brackets [].
  • You don't have a function and a variable with the same name.
  • You aren't overriding a built-in function and setting it to a tuple.
  • You don't have a class method and a property with the same name.
  • You aren't calling a function that returns a tuple twice.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

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.

Copyright ยฉ 2024 Borislav Hadzhiev