Typeerror tuple object is not callable error occurs because of calling any tuple as function. Which is technically not possible for Python Interpreter. There are Few more scenarios where we get this error. For Example, Using “tuple” as the variable name, Incorrectly accessing or declaring a list of the tuple objects, and typecasting of tuple object in the “str” object. Well, In this article we will address each scenario with a real example.
Firstly we will address the root cause. Then We will also address the other scenario.
Let’s jump to the code directly.
new_var=tuple((2,5,7))
#invoking tuple as function
new_var()
In the above code, Firstly we have declared and initialized the tuple. After it, we have called (invoked) it as a function. That is why the interpreter raises the error Typeerror tuple object is not callable.
Ideally, We can not use any python reserve keyword which declaring any variable name. Therefore, We should not use a tuple as a variable name. But Technically It is possible. So once we do it, We get the same error. For example-
tuple=(1,2,4)
new_var=tuple((2,5,7))
When we need to create a list of tuples. We need to be extra careful. Actually, We get the above error (tuple object is not callable) when we miss the comma separator in a list of tuples.
list_tup=[(1,3)(1,5)]
Lets take an example where we create a tuple object and typecast it as “str” object.
var=tuple(1,2,3)
var1=str(var)
Let’s see the output.
I hope now you will be able to understand the root cause of this error (tuple object is not callable).
Thanks
Data Science Learner Team