How To Solve “Attributeerror: ‘nonetype’ object has no attribute ‘#’” in Python

Attributeerror: ‘nonetype’ object has no attribute ‘#’

“Attributeerror: ‘nonetype’ object has no attribute ‘#’” is a common error related to Python class or object. The below explanations can help you know more about the cause of error and solutions.

How does the error “attributeerror: ‘nonetype’ object has no attribute ‘#’” happen?

NoneType means that what you have is not an instance of the class or object you think you are using. This is usually because an assignment or function call failed or returned an unexpected None value.

For example, attempting to access an attribute of a variable that is None will cause this error:

res = None

Here is another example of there might be some mistake in your code that makes it return None instead of another type:

def returnNone(something):

obj = {"x": "3"}
res = returnNone(obj)

Another reason is that you may have assigned in-place operations to mutable objects. For example:

List = list("7").sort()
print(List.x) # List is None

The sort() method always returns None if it is call by a list object. The reason behind this will be discussed in the solutions below.

How to solve this error?

Method 1: Make sure the value assigned to variables is not None

You must check carefully the value you have assigned to a variable before you access its attribute. Especially some methods will return a value None if there is an error in the method or if it has run successfully. Check what did the method return to make sure it is not None.

sampleList = list("7").sort()

if (sampleList is None):
	print("None detected")


None detected

The list is sorted in-place when using the sort() method of a list. Hence list is changed. The ordered list is not what the method returns; instead, it returns None. You turn the list to a None object. Therefore Python will give you warnings if you attempt to execute, for example, List.append(1) after the sort().

Method 2: Add a return statement to the functions or methods

Another way to fix this error is to check your function or method. If it lacks a return statement, then all you have to do is insert the return call with a value that is not of type None, such as an empty string or an empty list:

def returnSomething(something):
    return something

obj = {"x": "3"}
res = returnSomething(obj)



{'x': '3'}


We have learned how to deal with the error “attributeerror: ‘nonetype’ object has no attribute ‘#’”. You can easily avoid this error by finding and fixing a function that returns nothing.

Maybe you are interested:

Leave a Reply

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