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

avatar
Borislav Hadzhiev

Last updated: Apr 8, 2024
17 min

banner

# Table of Contents

  1. 'NoneType' object has no attribute 'X'
  2. 'NoneType' object has no attribute 'split'
  3. 'NoneType' object has no attribute 'get'
  4. 'NoneType' object has no attribute 'lower'
  5. 'NoneType' object has no attribute 'replace'
  6. NoneType object has no attribute 'encode'
  7. 'NoneType' object has no attribute 'items'

# 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 example.append('bobbyhadz.com')

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.

# A function that doesn't return anything returns None

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)

function returning none

Use an if statement if you need to check if a variable is not None before accessing an attribute.

main.py
example = 'bobbyhadz.com' if example is not None: # ๐Ÿ‘‡๏ธ this runs print('variable is not None') else: print('variable is None')

checking if variable is not none before accessing attribute

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.

The error 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.

# Many built-in methods return None

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 = ['bobby', 'hadz', 'com'] my_sorted_list = my_list.sort() print(my_sorted_list) # ๐Ÿ‘‰๏ธ None # โ›”๏ธ AttributeError: 'NoneType' object has no attribute 'pop' my_sorted_list.pop()

many built in methods return none

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.

# Checking if a variable is not None before accessing an attribute

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.

# Having a function that returns a value only if a condition is met

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 of whether the condition is met.

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.

# Concrete examples of solving the error

Here are some examples of solving the error for specific methods. Click on the subheading to navigate to the solution.

  1. 'NoneType' object has no attribute 'split'
  2. 'NoneType' object has no attribute 'get'
  3. 'NoneType' object has no attribute 'lower'
  4. 'NoneType' object has no attribute 'replace'
  5. NoneType object has no attribute 'encode'
  6. 'NoneType' object has no attribute 'items'

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

The Python "AttributeError: 'NoneType' object has no attribute 'split'" occurs when we try to call the split() method on a None value, e.g. assignment from a function that doesn't return anything.

To solve the error, make sure to only call split() on strings.

attributeerror nonetype object has no attribute split

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

main.py
my_string = None # โ›”๏ธ AttributeError: 'NoneType' object has no attribute 'split' result = my_string.split(',')

Use an if statement if you need to check if the variable is not None before calling str.split().

main.py
my_string = None if my_string is not None: print('Variable is not None') print(my_string.split(',')) else: print('Variable is None')

Trying to call the split() method on a None value causes the error.

If you print the variable you are calling split() on, it will be None, so you have to track down where the variable gets assigned a None value and correct or remove the assignment.

# A function that doesn't return a value returns None

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 get_string(): print('bobby,hadz,com') # ๐Ÿ‘‡๏ธ None my_string = get_string() # โ›”๏ธ AttributeError: 'NoneType' object has no attribute 'split' result = my_string.split(',')

Notice that our get_string 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 call split which caused the error.

The "AttributeError: 'NoneType' object has no attribute 'split'" 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.

# Checking if the variable is not None before calling split()

If a variable might sometimes store a string and sometimes store None, you can explicitly check if the variable is not None before you call split().

main.py
my_string = 'bobby,hadz,com' if my_string is not None: print('Variable is not None') result = my_string.split(',') print(result) # ๐Ÿ‘‰๏ธ ['bobby', 'hadz', 'com'] else: print('Variable is None')

The if block will run only if the my_string variable doesn't store a None value, otherwise, the else block runs.

Alternatively, you can check if the variable stores a string before calling str.split().

main.py
my_string = 'bobby,hadz,com' if isinstance(my_string, str): print('Variable is not None') result = my_string.split(',') print(result) # ๐Ÿ‘‰๏ธ ['bobby', 'hadz', 'com'] else: print('Variable is None')

The isinstance function returns True if the passed-in object is an instance or a subclass of the passed-in class.

# Having a function that returns a value only if a condition is met

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

main.py
def get_string(a): if len(a) > 3: return a my_string = get_string('hi') print(my_string) # ๐Ÿ‘‰๏ธ None

The if statement in the get_string function is only run if the passed in string has a length greater than 3.

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_string(a): if len(a) > 3: return a return '' my_string = get_string('hi') print(my_string) # ๐Ÿ‘‰๏ธ ""

Now the function is guaranteed to return a string regardless of whether the condition is met.

# The split() method should only be called on a string

The str.split() method splits the string into a list of substrings using a delimiter.

main.py
print('bobby,hadz,com'.split(',')) # ๐Ÿ‘‰๏ธ ['bobby', 'hadz', 'com'] print('bobby-hadz-com'.split('-')) # ๐Ÿ‘‰๏ธ ['bobby', 'hadz', 'com'] print('bobby hadz com'.split(' ')) # ๐Ÿ‘‰๏ธ ['bobby', 'hadz', 'com']

The method takes the following 2 parameters:

NameDescription
separatorSplit the string into substrings on each occurrence of the separator
maxsplitAt most maxsplit splits are done (optional)
When no separator is passed to the str.split() method, it splits the input string on one or more whitespace characters.

If the separator is not found in the string, a list containing only 1 element is returned.

  1. 'NoneType' object has no attribute 'get'
  2. 'NoneType' object has no attribute 'lower'
  3. 'NoneType' object has no attribute 'replace'
  4. NoneType object has no attribute 'encode'
  5. 'NoneType' object has no attribute 'items'

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

The Python "AttributeError: 'NoneType' object has no attribute 'get'" occurs when we try to call the get() method on a None value, e.g. assignment from function that doesn't return anything.

To solve the error, make sure to only call get() on dict objects.

attributeerror nonetype object has no attribute get

Here is an example of how the error occurs.

main.py
my_dict = None # โ›”๏ธ AttributeError: 'NoneType' object has no attribute 'get' my_dict.get('name') # โœ… If you need to check if not None before calling get if my_dict is not None: print('variable is NOT None') print(my_dict.get('hello')) else: print('variable is None')

Trying to call the get() method on a None value is what causes the error.

If you print the variable you are calling get() on, it will be None, so you have to track down where the variable gets assigned a None value and correct or remove the assignment.

# Functions that don't return anything return None

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 get_dict(): print({'name': 'Alice', 'age': 30}) # ๐Ÿ‘‡๏ธ None my_dict = get_dict() # โ›”๏ธ AttributeError: 'NoneType' object has no attribute 'get' my_dict.get('name')

Notice that our get_dict 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 call get() which caused the error.

# Common sources of None in Python

The error 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 mutate a data structure in place and don't return a value. In other words, they implicitly return None.

Make sure you aren't assigning the result of calling a method that returns None to a variable.

# Checking if a variable doesn't store None before calling get()

If a variable might sometimes store a dictionary and sometimes store None, you can explicitly check if the variable is not None before you call get().

main.py
my_dict = {'name': 'Alice', 'age': 30} if my_dict is not None: print('variable is NOT None') print(my_dict.get('name')) # ๐Ÿ‘‰๏ธ "Alice" else: print('variable is None')

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

# Functions that return a value only if a condition is met

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

main.py
def get_dict(a): if len(a) > 1: return a my_dict = get_dict({'name': 'Alice'}) print(my_dict) # ๐Ÿ‘‰๏ธ None

The if statement in the get_dict function is only run if the passed in dictionary has a length greater than 1.

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_dict(a): if len(a) > 1: return a return {} my_dict = get_dict({'name': 'Alice'}) print(my_dict) # ๐Ÿ‘‰๏ธ {}

Now the function is guaranteed to return a dictionary regardless of whether the condition is met.

  1. 'NoneType' object has no attribute 'lower'
  2. 'NoneType' object has no attribute 'replace'
  3. NoneType object has no attribute 'encode'
  4. 'NoneType' object has no attribute 'items'

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

The Python "AttributeError: 'NoneType' object has no attribute 'lower'" occurs when we try to call the lower() method on a None value, e.g. assignment from function that doesn't return anything.

To solve the error, make sure to only call lower() on strings.

attributeerror nonetype object has no attribute lower

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

main.py
my_string = None # โ›”๏ธ AttributeError: 'NoneType' object has no attribute 'lower' result = my_string.lower() # โœ… If you need to check if not None before calling lower() if my_string is not None: print('Variable is not None') print(my_string.lower()) else: print('Variable is None')

Trying to call the lower() method on a None value is what causes the error.

If you print the variable you are calling lower() on, it will be None, so you have to track down where the variable gets assigned a None value and correct or remove the assignment.

# Forgetting to return a value from a function

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 get_string(): print('BOBBY HADZ') # ๐Ÿ‘‡๏ธ None my_string = get_string() # โ›”๏ธ AttributeError: 'NoneType' object has no attribute 'lower' result = my_string.lower()

Notice that our get_string 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 call lower which caused the error.

The error 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.

# Checking if the variable is not None before calling lower()

If a variable might sometimes store a string and sometimes store None, you can explicitly check if the variable is not None before you call lower().

main.py
my_string = 'BOBBY HADZ' if my_string is not None: print('Variable is not None') result = my_string.lower() print(result) # ๐Ÿ‘‰๏ธ "bobby hadz" else: print('Variable is None')

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

# Returning a value only if a condition is met

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

main.py
def get_string(a): if len(a) > 3: return a my_string = get_string('hi') print(my_string) # ๐Ÿ‘‰๏ธ None

The if statement in the get_string function is only run if the supplied string has a length greater than 3.

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_string(a): if len(a) > 3: return a return '' my_string = get_string('hi') print(my_string) # ๐Ÿ‘‰๏ธ ""

Now the function is guaranteed to return a string regardless of whether the condition is met.

  1. 'NoneType' object has no attribute 'replace'
  2. NoneType object has no attribute 'encode'
  3. 'NoneType' object has no attribute 'items'

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

The Python "AttributeError: 'NoneType' object has no attribute 'replace'" occurs when we try to call the replace() method on a None value, e.g. assignment from a function that doesn't return anything.

To solve the error, make sure to only call replace() on strings.

attributeerror nonetype object has no attribute replace

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

main.py
my_string = None # โ›”๏ธ AttributeError: 'NoneType' object has no attribute 'replace' result = my_string.replace('z', 'a') # โœ… if you need to check if not None before calling replace() if my_string is not None: print('Variable is not None') print(my_string.replace('z', 'a')) else: print('Variable is None')

Trying to call the replace() method on a None value is what causes the error.

If you print the variable you are calling replace() on, it will be None, so you have to track down where the variable gets assigned a None value and correct or remove the assignment.

# Forgetting to return a value from a function

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 get_string(): print('zbc') # ๐Ÿ‘‡๏ธ None my_string = get_string() # โ›”๏ธ AttributeError: 'NoneType' object has no attribute 'replace' result = my_string.replace('z', 'a')

Notice that our get_string 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 call replace() which caused the error.

The error 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.

# Checking if a variable is not None before calling replace()

If a variable might sometimes store a string and sometimes store None, you can explicitly check if the variable is not None before you call replace().

main.py
my_string = 'one two four' if my_string is not None: print('Variable is not None') result = my_string.replace('four', 'three') print(result) # ๐Ÿ‘‰๏ธ "one two three" else: print('Variable is None')

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

# Returning a value only if a condition is met

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

main.py
def get_string(a): if len(a) > 3: return a my_string = get_string('hi') print(my_string) # ๐Ÿ‘‰๏ธ None

The if statement in the get_string function is only run if the passed in string has a length greater than 3.

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_string(a): if len(a) > 3: return a return '' my_string = get_string('hi') print(my_string) # ๐Ÿ‘‰๏ธ ""

Now the function is guaranteed to return a string regardless of whether the condition is met.

  1. NoneType object has no attribute 'encode'
  2. 'NoneType' object has no attribute 'items'

# AttributeError: NoneType object has no attribute 'encode'

The Python "AttributeError: 'NoneType' object has no attribute 'encode'" occurs when we try to call the encode() method on a None value, e.g. assignment from a function that doesn't return anything.

To solve the error, make sure to only call encode() on strings.

attributeerror nonetype object has no attribute encode

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

main.py
my_string = None # โ›”๏ธ AttributeError: 'NoneType' object has no attribute 'encode' result = my_string.encode('utf-8') # โœ… If you need to check if not None before calling encode() if my_string is not None: print('Variable is not None') print(my_string.encode('utf-8')) else: print('Variable is None')

Trying to call the encode() method on a None value is what causes the error.

If you print the variable you are calling encode() on, it will be None, so you have to track down where the variable gets assigned a None value and correct or remove the assignment.

# Forgetting to return a value from a function

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 get_string(): print('bobbyhadz.com') # ๐Ÿ‘‡๏ธ None my_string = get_string() # โ›”๏ธ AttributeError: 'NoneType' object has no attribute 'encode' result = my_string.encode('utf-8')

Notice that our get_string 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 call encode which caused the error.

The error 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.

# Checking if a variable is not None before calling encode()

If a variable might sometimes store a string and sometimes store None, you can explicitly check if the variable is not None before you call encode().

main.py
my_string = 'bobbyhadz.com' if my_string is not None: print('Variable is not None') result = my_string.encode('utf-8') print(result) # ๐Ÿ‘‰๏ธ b'bobbyhadz.com' else: print('Variable is None')

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

# Returning a value only if a certain condition is met

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

main.py
def get_string(a): if len(a) > 3: return a my_string = get_string('hi') print(my_string) # ๐Ÿ‘‰๏ธ None

The if statement in the get_string function is only run if the passed in string has a length greater than 3.

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_string(a): if len(a) > 3: return a return '' my_string = get_string('hi') print(my_string) # ๐Ÿ‘‰๏ธ ""

Now the function is guaranteed to return a string regardless of whether the condition is met.

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

The Python "AttributeError: 'NoneType' object has no attribute 'items'" occurs when we try to call the items() method on a None value, e.g. assignment from function that doesn't return anything.

To solve the error, make sure to only call items() on dict objects.

attributeerror nonetype object has no attribute items

Here is an example of how the error occurs.

main.py
my_dict = None # โ›”๏ธ AttributeError: 'NoneType' object has no attribute 'items' result = my_dict.items() if my_dict is not None: print('variable is NOT None') print(my_dict.items()) else: print('variable is None')

Trying to call the items() method on a None value is what causes the error.

If you print the variable you are calling items() on, it will be None, so you have to track down where the variable gets assigned a None value and correct or remove the assignment.

# Forgetting to return a value from a function

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 get_dict(): print({'name': 'Bobby Hadz', 'age': 30}) # ๐Ÿ‘‡๏ธ None my_dict = get_dict() # โ›”๏ธ AttributeError: 'NoneType' object has no attribute 'items' print(my_dict.items())

Notice that our get_dict 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 call items() which caused the error.

The error 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 mutate a data structure in place and don't return a value. In other words, they implicitly return None.

Make sure you aren't assigning the result of calling a method that returns None to a variable.

# Checking if a variable doesn't store None before calling items()

If a variable might sometimes store a dictionary and sometimes store None, you can explicitly check if the variable is not None before you call items().

main.py
my_dict = {'name': 'Bobby Hadz', 'age': 30} if my_dict is not None: print('variable is NOT None') print(my_dict.items()) # ๐Ÿ‘‰๏ธ dict_items([('name', 'Bobby Hadz'), ('age', 30)]) else: print('variable is None')

The if block will run only if the my_dict 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_dict(a): if len(a) > 1: return a my_dict = get_dict({'name': 'Bobby Hadz'}) print(my_dict) # ๐Ÿ‘‰๏ธ None

The if statement in the get_dict function is only run if the passed in dictionary has a length greater than 1.

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_dict(a): if len(a) > 1: return a return {} my_dict = get_dict({'name': 'Bobby Hadz'}) print(my_dict) # ๐Ÿ‘‰๏ธ {}

Now the function is guaranteed to return a dictionary regardless of whether the condition is met.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

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.

Copyright ยฉ 2024 Borislav Hadzhiev