2 Causes of TypeError: ‘Tuple’ Object is not Callable in Python

Introduction

In this article, we are exploring something new. From the title itself, you must be curious to know about the terms such as TypeError, tuple in python. So putting an end to your curiosity, let’s start with today’s tutorial on how to solve TypeError: ‘Tuple’ Object is not Callable in Python.

What is tuple in python?

A tuple is one of the four in-built data structures provided by python. It is a collection of elements that are ordered and are enclosed within round brackets (). A tuple is immutable, which means that it cannot be altered or modified. Creating a tuple is simple, i.e., putting different comma-separated objects. For example: – T1 = (‘Chanda’, 11, ‘Kimmi’, 20).

Causes of TypeError: 'Tuple' Object is not Callable in Python
Tuple in python

What is Exception in python?

Sometimes we often notice that even though the statement is syntactically correct in the code, it lands up with an error when we try to execute them. These errors are known as exceptions. One of these exceptions is the TypeError exception. Generally, other exceptions, including the TypeError exception, are not handled by program. So let’s do some code to understand exceptions.

120 * (1/0)
OUTPUT: - Traceback (most recent call last):  
                       File "<stdin>", line 1, in <module>
                         ZeroDivisionError: division by zero

The last line of the error message indicates what kind of exception has occurred. The different types of exceptions are:

  • ZeroDivisionError
  • NameError
  • TypeError

What is TypeError Exception in Python?

TypeError exception occurs when an operation is performed to an object of inappropriate data type. For example, performing the addition operation on a string and an integer value will raise the TypeError exception. Let’s do some code to have a clear understanding.

str = 'Favorite'
num = 5
print(str + num + str)
OUTPUT: - TypeError: Can't convert 'int' object to str implicitly  

In the above example, the variable ‘str’ is a string, and the variable ‘num’ is an integer. The addition operator cannot be used between these two types, and hence TypeError is raised.

Let us understand different type of TypeError exception which is Incorrect type of list index.

list1 = ["physics", "chemistry", "mathematics", "english"]
index = "1"
print(list1[index])
OUTPUT: - TypeError: list indices must be integers or slices, not str

In the above-written Python code, the list index must always be an integer value. Since the index value used is a string, it generates a TypeError exception.

Till now, you must have understood the TypeError exception. Now let’s dive deeper into the concept of TypeError exception occurring in a tuple or due to a tuple.

TypeError: ‘tuple’ object is not callable

You must be wondering why this type of TypeError occurs. This is because tuples are enclosed with parenthesis creates confusion as parenthesis is also used for a function call wherein we pass parameters. Therefore, if you use parenthesis to access the elements from a tuple or forget to separate tuples with a comma, you will develop a “TypeError: ‘tuple’ object not callable” error. There are two causes for the “TypeError: ‘tuple’ object is not callable” error, and they are the following:

  • Defining a list of tuples without separating each element with a comma.
  • Using the wrong syntax for indexing.

Let’s us discuss in detail.

Cause 1. Missing Comma

Sometimes the “TypeError: ‘tuple’ object is not callable” error is caused because of a missing comma. Let’s start to code to understand this.

marks = [
	("Kimmi", 72),
	("chanda", 93)
	("Nupur", 27)
]
print(marks)
OUTPUT:-

Traceback (most recent call last):                                                                                                        
  File "main.py", line 4, in <module>                                                                                                     
    ("Nupur", 27)                                                                                                                       
TypeError: 'tuple' object is not callable 

As expected, an error was thrown. This is because we have forgotten to separate all the tuples in our list with a comma. When python sees a set of parenthesis that follows a value, it treats the value as a function to call.

Cause 2: Incorrect syntax of an index

Let’s us first code to understand this cause.

marks = [
	("Kimmi", 72),
	("chanda", 93),
	("Nupur", 27)
]
for i in marks:
    print("Names: " +str(i(0)))
    print("Marks: " +str(i(1)))
OUTPUT: - Traceback (most recent call last):                                                                                                            
  File "main.py", line 7, in <module>                                                                                                        
    print("Names: " +str(i(0)))                                                                                                               
TypeError: 'tuple' object is not callable 

The above loop should print each value from all the tuples in the “marks” list. We converted each value to a string so that it is possible to concatenate them to the labels in our print statements, but our code throws an error.

The reason behind this is that we are trying to access each item from our tuple using round brackets. While tuples are defined using round brackets, i.e., (), their contents are made accessible using traditional indexing syntax. Still, the tuples are defined using round brackets. Therefore, their contents are made accessible using traditional indexing syntax.

You must be thinking now about what we should do to make our code executable. Well, it’s simple. But, first, we have to use square brackets [ ] to retrieve values from our tuples. So, Let’s look at our code.

marks = [
	("Kimmi", 72),
	("chanda", 93),
	("Nupur", 27)
]
for i in marks:
    print("Names: " +str(i[0]))
    print("Marks: " +str(i[1]))
OUTPUT: - 

Names: chanda                                                                                                                                 
Marks: 93                                                                                                                                     
Names: Nupur                                                                                                                                  
Marks: 27   

Our code successfully executes the information about each student.

Also Read | NumPy.ndarray object is Not Callable: Error and Resolution

What objects are not callable in Python?

Previously, we discussed the Tuple object is not callable in python; other than that, we also have another object which is not callable, and that is the list.

1. typeerror: ‘list’ object is not callable 

When you try to access items in a list using round brackets (), Python returns an error called the typeerror. This is because Python thinks that you are trying to call a function.

The solution to this problem is to use square brackets [ ] to access the items in a list. We know that round brackets are usually used to call a function in python.

2. typeerror: ‘module’ object is not callable 

While using the functions, we also use modules to import and then use them. This might create confusion. because, in some cases, the module name and function name may be the same. For example, the getopt module provides the getopt() function, which may create confusion. Callable means that a given python object can call a function, but in this error, we warned that a given module could not be called like a function.

The solution to this problem is that we will use from and import statements. from is used to specify the module name, and import is used to point to a function name.

3. typeerror: ‘int’ object is not callable

Round brackets in Python have a special meaning. They are used to call a function. If you specify a pair of round brackets after an integer without an operator between them, the Python interpreter will think that you’re trying to call a function, and this will return a “TypeError: ‘int’ object is not callable” error.

4. typeerror: ‘str’ object is not callable

Mistakes are often committed, and it is a human error. Therefore, our error message is a TypeError. This tells us that we are trying to execute an operation on a value whose data type does not support that specific operation. From the above statement, I meant that when you try to call a string like you would a function, an error will be returned. This is because strings are not functions. To call a function, you add round brackets() to the end of a function name.

This error occurs when you assign a variable called “str” and then try to use the function. Python interprets “str” as a string, and you cannot use the str() function in your program.

Also, Read | How to Solve TypeError: ‘int’ object is not Subscriptable

Conclusion

The “TypeError: ‘tuple’ object is not callable” error occurs 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.

Ensure that when you access items from a tuple, you use square brackets and ensure that all tuples in your code should be separated with a comma.

Now you’re ready to fix this error in your code like a professional Python developer!. Till then, keep reading articles.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments