AttributeError: ‘str’ object has no attribute ‘append’

The AttributeError in python is defined as an error that occurs when a reference is made to an unassociated attribute of a class or when an assignment is made with an unassociated attribute of a class. The AttributeError is raised when an invalid class attribute is used for reference or assignment.

The python class is a collection of data and functionality. The object in python is an enclosed collection of data and functionality identified as a class variable. The attribute in python is the collection of class-related data and functionality. These attributes are available for all class objects. The Attribute error is thrown if an unassociated attributes are invoked in a class.

The dot operator is used to reference to a class attribute. The reference attribute is made with an attribute that is not available in a class that throws the attribute error in python. The assignment is made with an attribute that is not associated with the class, which will also cause the attribute error “AttributeError: ‘str’ object has no attribute ‘append'”.

In this article, we will find out about the python attribute error, what are all the different types of attribute error, the root cause of the attribute error, when this attribute error occurs, and how to fix this attribute error in python.



Different Attribute Error Variation

The attribute error “AttributeError: ‘type’ object has no attribute ‘x’ ” is shown with the object type and the attribute name. The error message will be shown as below

AttributeError: 'str' object has no attribute 'append'
AttributeError: 'int' object has no attribute 'append'
AttributeError: 'long' object has no attribute 'append'
AttributeError: 'float' object has no attribute 'append'
AttributeError: 'bool' object has no attribute 'append'
AttributeError: 'NoneType' object has no attribute 'append'
AttributeError: 'complex' object has no attribute 'append'
AttributeError: 'module' object has no attribute 'append'
AttributeError: 'class' object has no attribute 'append'
AttributeError: 'list' object has no attribute 'size'
AttributeError: 'tuple' object has no attribute 'size'
AttributeError: 'set' object has no attribute 'size'
AttributeError: 'dict' object has no attribute 'size'


Exception

If the python interpreter throws an attribute error, the attribute error “AttributeError: ‘type’ object has no attribute ‘x'” will be shown as below. The attribute error will show the type of the object from which it is ejected and the name of the attribute.

Traceback (most recent call last):
  File "/Users/python/Desktop/test.py", line 2, in <module>
    a.append(2)
AttributeError: 'str' object has no attribute 'append'
[Finished in 0.0s with exit code 1]


Root Cause

The python class consists of a collection of properties and actions. The object in python is an enclosed collection of properties and functions identified as a class variable. In python, the class properties and functions are referred as the class attribute.

If the attribute is invoked from an unrelated class, the python interpreter can not identify the attribute in the class. As a result, the python interpreter throws this attribute error.



How to reproduce this issue

If the unassociated attribute is referenced in the class, or if the assignment is made with the unassociated attribute of the class, the python interpreter throws the error of the attribute.

In the example below the python string invokes the append attribute. The python string does not support the attribute of the append. As a result, the python interpreter throws the attribute error.

Program

a = 'Hello'
a.append(' World')

Output

Traceback (most recent call last):
  File "/Users/python/Desktop/test.py", line 2, in <module>
    a.append(' World')
AttributeError: 'str' object has no attribute 'append'
[Finished in 0.1s with exit code 1]


Solution 1

You are referring an attribute that may be relevant to other languages such as java, c++, dot net etc. Refer the python manual for the supported attributes of the class. If you’re keen on specific features in python, refer to the python manual for this.

In the example below a variable contains a “Hello” string. Append method to concatenate with another “world” string is invoked In python, two strings are concatenated by using the arithmetic addition operator. This attribute error is fixed by replacing the append attribute with the arithmetic addition operator.

Program

a = 'Hello'
a = a +' World'
print a

Output

Hello World


Solution 2

You may refer to an attribute that belongs to another class, or the visibility of the attribute is not visible in the class. Check the attribute available in the class and make sure that it has proper visibility, such as public or protected.

In the example below, the python file Employee.py contains an Employee class. The Employee class is imported in the test.py file. The Employee class contains an attribute called “id” that is available in public access scope. the object of the Employee class is used to reference the attribute “id”.

Employee.py

class Employee:
	id = 1

test.py

from Employee import Employee

emp = Employee();
print(emp.id);

Output

1



Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *