You will probably encounter the error AttributeError: ‘tuple’ object has no attribute in Python a few times when working with python datatype. So, read this article if you do not know how to fix it.
AttributeError: ‘tuple’ object has no attribute in Python – When does it happen?
This error will appear when you accidentally access an attribute that does not exist in a tuple. For example, let’s see the code below to understand.
def create_something(): str1 = "learn" str2 = "share" str3 = "IT" return str1, str2, str3 something = create_something() print(something.str1) # Expected output: 'learn'
Output:
AttributeError: 'tuple' object has no attribute 'str1'
How to fix it?
We can easily avoid this error by not accessing a tuple’s null property.
Here are some of the most straightforward ways:
Variable is variable, definitely not an attribute!
Take a look at the example above. The person who wrote that code must be confused between variables and attributes.
The create_something()
function returns a tuple that cannot be accessed by text.
Let’s see the revised code below.
def create_something(): str1 = "learn" str2 = "share" str3 = "IT" return str1, str2, str3 something = create_something() # something = ('learn', 'share' , 'IT') print(something[0])
Output:
learn
Use the appropriate datatype for the attribute
You cannot use text to access the element if a variable is a tuple. Only the index is allowed to access it.
If you get the above error, you are definitely using a tuple and accessing an attribute that is not in the tuple. So, let’s look at the code to know how to use the data type appropriately.
def create_something(): return 'learn', 'and', 'share', 'IT' something = create_something() # Use List instead of Tuple list_something = list(something) list_something.remove('and') print(list_something)
Output:
['learn', 'share', 'IT']
Use the hasattr() function
Syntax:
hasattr(object, attribute)
Parameters:
- object: An object has an attribute to be checked.
- attribute: A property needs to be checked.
hasattr()
function returns true if the attribute exists in the object and returns false if the attribute does not exist in the object. So, we can use it to check before using an attribute on an object.
def create_something(): return 'learn', 'and', 'share', 'IT' something = create_something() if hasattr(something, 'remove'): something.remove('and') else: print("Something doesn't have this attribute; use another datatype that has this attribute")
Output:
Something doesn't have this attribute; use another datatype that has this attribute
Summary
“AttributeError: ‘tuple’ object has no attribute” in Python is not difficult to fix as long as you have basic knowledge of datatype and practice regularly with Python data types. After reading this article, we hope you can avoid this and similar errors in the future. Thank you for reading!
Maybe you are interested:
- AttributeError: module ‘aiobotocore’ has no attribute ‘AioSession’
- AttributeError: module ‘keras.engine’ has no attribute ‘Layer’ in Python
- AttributeError: partially initialized module has no attribute

Hi, I’m Cora Lopez. I have a passion for teaching programming languages such as Python, Java, Php, Javascript … I’m creating the free python course online. I hope this helps you in your learning journey.
Name of the university: HCMUE
Major: IT
Programming Languages: HTML/CSS/Javascript, PHP/sql/laravel, Python, Java