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

The AttributeError: ‘NoneType’ object has no attribute ‘append’ error happens when the append() attribute is called in the None type object. The NoneType object has no attribute like append(). That’s where the error AttributeError: ‘NoneType’ object has no attribute ‘append’ has happened.

The python variables, which have no value initialised, have no data type. These variables are not assigned any value, or objects. These python variable does not support append() attribute. when you call append() attribute in a None type variable, the exception AttributeError: ‘NoneType’ object has no attribute ‘append’ will be thrown.

If nothing is assigned to the python variable, the variable can not be used unless any value or object is assigned. Calling the attribute of this variable value is pointless. If you call an attribute like append() the exception AttributeError: ‘NoneType’ object has no attribute ‘append’ will be thrown.



Exception

If python throws the attribute error, the error stack will be seen as below. The AttributeError: ‘NoneType’ object has no attribute ‘append’ error would display the line where the error happened.

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


How to reproduce this issue

If a python variable is created without assigning an object or value, it contains None. If the attribute is called with the python variable, the error will be thrown. The exception is thrown by calling an attribute from a variable that has no object assigned to it.

Program

a = None;
a.append(' World')
print a

Output

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


Root Cause

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 a variable has no object assigned is invoked.

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 attribute is called in a variable that is not associated with any object of the class, which will also cause the attribute error AttributeError: ‘NoneType’ object has no attribute ‘append’.



Solution 1

The none type variable must be assigned with a value or object. If the variable has valid object that contains attributes such as append(), the error will be resolved. Otherwise, the variable is used with basic python operators such as arithmetic operator.

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

If the python variable does not required to assign a value, the python variable should be assigned with empty list. The empty list will add a value if the append() function is called in the code. The example below contains a python variable that is assigned with an empty list. if the append() function is called, the value is added in the empty list.

Program

a = []
a.append(' World')
print a

Output

[' World']
[Finished in 0.0s]


Solution 3

Due to the dynamic creation of the variable the python variable may not be assigned with values. The datatype of the variable is unknown. In this case, the None data type must be checked before the an attribute is called.

Program

a = None;
if a is not None:
	a.append(' World')
print a

Output

None
[Finished in 0.1s]


Solution 4

The python variable should be validated for the expected data type. If the variable has the expected data type, then the object attribute should be invoked. Otherwise, the alternate flow will be invoked.

If the variable contains the excepted type of the value, the if block will execute. Otherwise, the else block will execute. This will eliminate the error AttributeError: ‘NoneType’ object has no attribute ‘append’.

Program

a = [];
if type(a) is list:
	a.append(' World')
else :
	a =a;
print a

Output

[' World']
[Finished in 0.0s]


Solution 5

If the data type of the variable is unknown, the attribute will be invoked with try and except block. The try block will execute if the python variable contains value or object. Otherwise, the except block will handle the error.

In the example below, the append attribute is called within the try block. If any error occurs the except block will be executed. The error will not be thrown.

Program

a = None;
try :
	a.append(' World')
except :
	print 'error';
print a

Output

error
Hello
[Finished in 0.0s]



Leave a Reply