A tuple in Python stores multiple collections of data, just like a list, a set, and a dictionary. Python programs may encounter TypeErrors when inappropriate operations are performed on these data types or when the operation is not supported for the object.
One such TypeError, “tuple object is not callable in Python”, occurs due to various reasons. This write-up will explain all the possible reasons and appropriate solutions for “TypeError: tuple object is not callable” in Python with examples. The following aspects will be discussed one by one in this article:
- Reason 1: Incorrect Accessing Tuple
- Solution: Use a Square Bracket Instead of a Parenthesis
- Reason 2: Overriding tuple() Function
- Solution: Rename Tuple Variable
- Reason 3: Incorrect Initialization of Two Tuple
- Solution: Separate Multiple Tuple by Comma
- Reason 4: Calling Tuple as a Function
- Solution: Remove Parenthesis
So, let’s get started!
Reason 1: Incorrect Accessing Tuple
One prominent reason that causes this error is accessing tuples incorrectly in Python. In the below snippet, the tuple value is accessed using parentheses instead of square brackets, which raises a “TypeError” in the program:
The output shows a TypeError because the tuple item/element was accessed incorrectly.
Solution: Use a Square Bracket Instead of Parentheses
While accessing the elements of a tuple using an index, users must use the square bracket instead of the parentheses.
tuple_val = ('Alan', 'Benny', 'Cyndy') print(tuple_val) print(tuple_val) print(tuple_val)
In the above code, the tuple value is accessed using square brackets.
The output proves that the tuple items have been accessed successfully.
Note: Make sure the accessed tuple index must be in the range.
Reason 2: Overriding tuple() Function
Another common reason that causes the stated error is the overriding of the tuple() function in Python programs:
The above output shows the TypeError due to the overriding of the tuple() function.
Solution: Rename Tuple Variable
To solve the “tuple object is not callable” error, the tuple variable must be renamed in the program.
tuple_val = ('Alan', 'Benny', 'Cyndy') output = tuple(['a', 's', 'b']) print(output)
In the above code, the tuple variable has been renamed from “tuple” to “tuple_val”.
Assigning different names to the tuples resolves the stated issue.
Reason 3: Incorrect Initialization of Two Tuples
If we initialize two tuples next to each other without comma-separated syntax, then the error “tuple object is not callable” occurs in the Python script:
In the above snippet, we encountered an error because the tuples were not separated by a comma.
Solution: Separate Multiple Tuples by Comma
To solve this error, the tuple value must be separated by a comma.
tuple_val = ('Alan', 'Benny', 'Cyndy'),('Alex', 'David', 'Carry') print(tuple_val)
In the above code, the two tuple values are initialized using comma-separated syntax and stored in a variable named “tuple_val”.
The output shows that placing a comma between the tuples resolves the TypeError.
Reason 4: Calling Tuple as a Function
One of the main reasons that cause this “TypeError” in Python is calling a tuple a function.
In the above snippet, the TypeError occurs because the tuple value is accessed as a function.
Solution: Remove Parenthesis
To rectify this error, access the tuple without parentheses. To access a specific tuple item, specify the index number within the square bracket.
tuple_val = ('Alan', 'Benny', 'Cyndy') print(tuple_val)
In the above code, the tuple is accessed using square brackets and shows the tuple value on screen using the print() function.
The output authenticates that removing the parenthesis resolves the specified error.
Note: Avoid the same name for variables and functions; otherwise, the “TypeError” will occur in the program.
That’s it from this guide!
The error “tuple object is not callable” occurs when the tuple is accessed incorrectly, calling the tuple as a function, overriding the tuple, etc. Also, the stated error occurs if more than one tuple is not separated by a comma. To access tuples, do not use the parentheses instead, access the tuple elements using square brackets. This article presented various reasons and solutions for the “tuple object is not callable” error in Python.