Python AttributeError: 'tuple' object has no attribute
This error occurs when attempting to access the values of a tuple incorrectly.
Functions that return multiple variables will output the results in a tuple, so we cannot use dot-access to retrieve the values.
For example, let's create a simple function that returns two values:
If we call the function and attempt to access the tuple's elements with dot-access, i.e. as an attribute, we will see the error:
As we can see from the error message, the attribute
val_1 does not exists for the tuple.
What's an attribute?
What does the error mean by attribute? An example of an attribute would be class variables. For example, say we have a simple
We can see in this example that
kind is an attribute of the
Tree class, and we can access it with a dot.
In contrast, to correctly access the tuple's value, we need to use an index:
Or, alternatively, by using tuple unpacking:
Often this error occurs when using an external library where you didn't know a function returned a tuple. We'll describe examples of this below.
If you create your own function and want your return value to have dot-access, you can use a
namedtuple or a class.
Cause 1: Treating Tuple Values as Named Attributes
As mentioned previously, any function which returns multiple values will output them as a tuple-type by default, which cannot be dot-accessed.
To demonstrate this, let's write a small function called
perform_calculations that takes two numbers and outputs values for their sum, product, and quotient:
Python packs function outputs into a tuple, as shown by the parentheses containing the sequence. We know that one of the function's outputs is the product of the two numbers, so let's try and access that:
results variable is a tuple, so we can't access specific values in the manner shown above.
One way of assigning tuple values to variables is to unpack the tuple:
The solution shows that tuple values need to be unpacked and assigned to variables before directly accessing them. Note that we can unpack our function output straight into
div, without having to use
results as a go-between:
This article contains a few more examples of unpacking functions and lists.
We could also use indices to reference specific tuple values:
Optionally, if you would like to have dot-access to elements, you can use a
namedtuple like so:
Similarly, this could be accomplished by using a class.
We've been using a function we created in these examples, but this error often shows up when using third-party libraries, like in the following section.
Cause 2: Not Unpacking iterrows()
This error is seen frequently while using the pandas
iterrows() DataFrame method.
Let's take another look at the Iris dataset used in the introduction, iterating through the first three rows of the dataframe using
Initially, you might assume
iterrows() would return a dot-accessible DataFrame row. Let's try:
We can see
row is a tuple, so let's print the rows to examine how to unpack them:
We can see
iterrows() generates a tuple that looks like
(index, row contents).
As a result, trying to access specific row features without unpacking them first will throw an error. We need to unpack each row like so:
Alternatively, you could specify that you're only interested in the second value of the tuple using indices, which would prevent you from needing to unpack the tuple.
For example, we could index the
iterrows() output as follows:
The two solutions effectively do the same thing, but developers use the first approach more commonly as it is slightly more readable. Furthermore, you can throw away the index with an underscore if you don't need it:
The underscore conveys to readers of your code that you intend not to use the index value.
AttributeError: 'tuple' object has no attribute is caused when treating the values within a tuple as named attributes. The error also occurs very frequently when using
iterrows(), hence the big focus on avoiding the error in that case.
It's important to remember that any functions returning multiple values will automatically store the outputs in a tuple, which could also potentially cause this error to come up. Luckily, the error is pretty easy to avoid in most cases by unpacking the tuple first, assigning the values to variables. Alternatively, using indices to index a tuple is also a viable solution.