One of the built-in data structures of Python is a tuple, hence the Typeerror: ‘tuple’ object is not callable is an error we can’t avoid encountering.
So in this guide, we will know better what this error means and what causes it. We will also provide examples and solutions to solve this error.
Prior to that, here is a quick review of what is a tuple.
Understanding Tuple
Tuple is one of the four built-in data structures of Python. Wherein it consists of a group of items that are enclosed and grouped together in parenthesis ( ).
In addition, a tuple is immutable in other words it can not be changed in any way.
Since it is enclosed within parentheses. As a result, it can be confusing since functions also use parenthesis ( ).
Therefore if there is no comma (,) to separate the items or parenthesis is used to access an item. The TypeError: ‘tuple’ object is not callable” error is thrown.
What is ‘tuple’ object is not callable?
The TypeError: ‘tuple’ object is not callable error occurs when we call a tuple as if it is a function.
Apart from it, we used parenthesis ( ) instead of square brackets [ ] when accessing the tuples specific index.
Causes of Typeerror: ‘tuple’ object is not callable
Here are other potential causes why this Typeerror: ‘tuple’ object is not callable occurs:
- When accessing a tuple we use parentheses ( ) instead of square brackets [ ].
- Variable names you have and functions have the same name.
- By mistake built-in function override and sets it to a tuple.
- A function is called and returns a tuple twice.
- Incorrect indexing syntax
- A missing comma (,) before a nested tuple
Typeerror: ‘tuple’ object is not callable – Solutions
Here are the following solutions you can try to fix Typeerror: ‘tuple’ object is not callable error.
Check a Missing Comma
The most common mistake why we get this Typeerror: ‘tuple’ object is not callable error is we missed a comma ‘,’.
Like for instance, this tuple which stores information about the salary.
Salary = [
("Ben", 72, 1190),
("Ten", 93, 2110)
("Tim", 127, 2130)
]
In the code above, the tuple first value has the name of a person, then the number of days of work, and lastly the salary.
If we run the code, we will get the following error since we forgot the comma to separate the tuples in our list…
Output:
("Ten", 93, 2110) Traceback (most recent call last): File "C:\Users\Windows\PycharmProjects\pythonProject1\main.py", line 3, in <module> ("Ten", 93, 2110) TypeError: 'tuple' object is not callable
In order to fix this we need to make sure that all the values in our list of tuples are separated by a comma (,).
Examine the example code below:
Salary = [
("Ben", 72, 1190),
("Ten", 93, 2110),
("Tim", 127, 2130)
]
print(Salary)
As we can see we added a comma (,) after the tuple of Ten values. If we run this code, the error will be fixed.
Output:
[('Ben', 72, 1190), ('Ten', 93, 2110), ('Tim', 127, 2130)]
Incorrect indexing syntax
Another way to fix the error is to have a correct indexing syntax. Assuming we have this code that will display the value of the tuple list.
However, we use the parenthesis in accessing items from our tuple.
Salary = [
("Ben", 72, 1190),
("Ten", 93, 2110),
("Tim", 127, 2130)
]
for s in Salary:
print("Employee Name: " + str(s(0)))
print("Days: " + str(s(1)))
print("Salary: $" + str(s(2)))
Hence if we run the code this will throw an error.
To fix this error we need to use the square brackets [ ] in retrieving values in our tuples.
Salary = [
("Ben", 72, 1190),
("Ten", 93, 2110),
("Tim", 127, 2130)
]
for s in Salary:
print("Employee Name: " + str(s[0]))
print("Days: " + str(s[1]))
print("Salary: $" + str(s[2]))
In the code above, we use for loop to display the information and print all the values of the tuple in the Salary list. As we can see we convert the value to a string in order to concatenate them to the labels in the print method.
Output:
Employee Name: Ben Days: 72 Salary: $1190 Employee Name: Ten Days: 93 Salary: $2110 Employee Name: Tim Days: 127 Salary: $2130
Calling a method that’s also the name of a property
Having the same name of class property constructor will shadow other attributes having the same name once you define it.
For instance, we have a property named job which is a tuple to keep the jobs‘ names. Further, we defined a method, also named job.
Unfortunately, this will cause an error.
Here is the example code:
class Job:
def __init__(self, employee, job):
self.employee = employee
self.job = job
def job(self):
return self.job
job = ('Teacher', 'Writer')
book = Job('Head First Python', job)
print(book.job())
To fix this error the name get_job is an alternative that sounds safer and more readable.
class Job:
def __init__(self, employee, job):
self.employee = employee
self.job = job
def get_job(self):
return self.job
job = ('Teacher', 'Writer')
book = Job('Head First Python', job)
print(book.get_job())
Output:
('Teacher', 'Writer')
Conclusion
The TypeError: ‘tuple’ object is not callable error is raised when you try to call a tuple as a function. This can happen if you use the wrong syntax to access an item from a tuple or if you forget to separate two tuples with a comma (,).
By following the outlined solutions above, surely you’ll be able to fix the error.
We hope that this guide has helped you resolve this error and get back to coding.
If you are finding solutions to some errors you might encounter we also have Typeerror: ‘dataframe’ object is not callable.
Thank you for reading!