AttributeError: 'NoneType' object has no attribute 'X'

avatar

Borislav Hadzhiev

Last updated: Apr 20, 2022

banner

Photo from Unsplash

AttributeError: 'NoneType' object has no attribute 'X' #

The Python "AttributeError: 'NoneType' object has no attribute" occurs when we try to access an attribute on a None value, e.g. assignment from a function that doesn't return anything. To solve the error, correct the assignment before accessing the attribute.

attributeerror nonetype object has no attribute

Here is a very simple example of how the error occurs.

main.py
example = None # ⛔️ AttributeError: 'NoneType' object has no attribute 'append' example.append('hello')

Trying to access or set an attribute on a None value causes the error.

If you print the variable you are accessing the attribute on, it will be None, so you have to track down where the variable gets assigned a None value and correct the assignment.

The most common source of a None value (other than an explicit assignment) is a function that doesn't return anything.

main.py
# 👇️ this function returns None def do_math(a, b): print(a * b) # 👇️ None example = do_math(10, 10) # ⛔️ AttributeError: 'NoneType' object has no attribute 'my_attribute' print(example.my_attribute) # ✅ use this if you need to check if a variable is not None # before accessing an attribute if example is not None: print('variable is not None') else: print('variable is None')

Notice that our do_math function doesn't explicitly return a value, so it implicitly returns None.

We assigned the result of calling the function to a variable and tried to access an attribute which caused the error.

The "AttributeError: 'NoneType' object has no attribute" occurs for multiple reasons:

  1. Having a function that doesn't return anything (returns None implicitly).
  2. Explicitly setting a variable to None.
  3. Assigning a variable to the result of calling a built-in function that doesn't return anything.
  4. Having a function that only returns a value if a certain condition is met.

Some built-in methods (e.g. sort) mutate a data structure in place and don't return a value. In other words, they implicitly return None.

main.y
my_list = ['c', 'b', 'a'] my_sorted_list = my_list.sort() print(my_sorted_list) # 👉️ None # ⛔️ AttributeError: 'NoneType' object has no attribute 'pop' my_sorted_list.pop()

The sort() method sorts a list in place and doesn't return anything, so when we assign the result of calling the method to a variable, we assign a None value to the variable.

Avoid assignment with methods that mutate the original object in place, as most of them don't return a value and implicitly return None.

If a variable might sometimes store a real value and sometimes store None, you can explicitly check if the variable is not None before you access an attribute.

main.py
example = None if example is not None: print('variable is not None') else: print('variable is None') # 👉️ this runs

The if block will run only if the example variable does not store a None value, otherwise the else block runs.

Another common cause of the error is having a function that returns a value only if a condition is met.

main.py
def get_num(a): if a > 100: return a result = get_num(5) print(result) # 👉️ None

The if statement in the get_num function is only run if the passed in argument is greater than 100.

In all other cases, the function doesn't return anything and ends up implicitly returning None.

To solve the error in this scenario, you either have to check if the function didn't return None, or return a default value if the condition is not met.

main.py
def get_num(a): if a > 100: return a return 0 # 👈️ returns 0 if condition not met result = get_num(5) print(result) # 👉️ 0

Now the function is guaranteed to return a value regardless if the condition is met.

Conclusion #

The Python "AttributeError: 'NoneType' object has no attribute" occurs when we try to access an attribute on a None value, e.g. assignment from a function that doesn't return anything. To solve the error, correct the assignment before accessing the attribute.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.