Last updated: Apr 8, 2024
Reading timeยท5 min
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]
.
Here is one example of how the error occurs.
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 when accessing elements of a tuple.
my_tuple = ('a', 'b', 'c', 'd') print(my_tuple[0]) # ๐๏ธ 'a' print(my_tuple[1]) # ๐๏ธ 'b' print(my_tuple[2]) # ๐๏ธ 'c'
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.
my_tuple = ('a', 'b', 'c', 'd') print(my_tuple[0:2]) # ๐๏ธ ('a', 'b') print(my_tuple[2:4]) # ๐๏ธ ('c', 'd')
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.
The error occurs for multiple reasons:
()
instead of square brackets
[]
.tuple()
functionAnother thing to look out for is that you're not overriding the built-in tuple() function.
# ๐๏ธ 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.
# โ No longer overriding the 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.
# โ๏ธ 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.
my_list = [('a', 'b'), ('c', 'd')]
Another common cause of the error is simply trying to call a tuple as a function.
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.
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.
Make sure you don't have a function and a variable with the same name.
def example(): return 'bobbyhadz.com' example = ('a', 'b', 'c') # โ๏ธ TypeError: 'tuple' object is not callable print(example())
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.
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.
The error is also caused when we have a class method and a class property with the same name.
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.
To solve the error, you have to rename the class method.
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.
The error also occurs when you call a function that returns a tuple twice.
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.
def my_func(): return ('bobby', 'hadz', 'com') print(my_func()) # ๐๏ธ ('bobby', 'hadz', 'com')
In case you declared a tuple by mistake, tuples are constructed in multiple ways:
()
creates an empty tuplea,
or (a,)
a, b
or (a, b)
tuple()
constructorTo solve the "TypeError: 'tuple' object is not callable" error, make sure:
()
instead of square
brackets []
.You can learn more about the related topics by checking out the following tutorials: