Last updated: Apr 8, 2024
Reading timeยท17 min
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.
Here is a very simple example of how the error occurs.
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.
None
, so you have to track down where the variable gets assigned a None
value and correct the assignment.None
The most common source of a None
value (other than an explicit assignment) is
a function that doesn't return anything.
# ๐๏ธ 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 an if
statement if you need to
check if a variable is not None before accessing
an attribute.
example = 'bobbyhadz.com' if example is not None: # ๐๏ธ this runs 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.
The error occurs for multiple reasons:
None
implicitly).None
.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
.
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()
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.
None
.None
before accessing an attributeIf 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.
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.
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
.
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.
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:
None
implicitly).None
.Here are some examples of solving the error for specific methods. Click on the subheading to navigate to the solution.
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.
Here is a very simple example of how the error occurs.
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()
.
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.
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.None
The most common source of a None
value (other than an explicit assignment) is
a function that doesn't return anything.
# ๐๏ธ 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
.
split
which caused the error.The "AttributeError: 'NoneType' object has no attribute 'split'" occurs for multiple reasons:
None
implicitly).None
.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()
.
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()
.
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.
Another common cause of the error is having a function that returns a value only if a condition is met.
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
.
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.
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 str.split() method splits the string into a list of substrings using a delimiter.
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:
Name | Description |
---|---|
separator | Split the string into substrings on each occurrence of the separator |
maxsplit | At most maxsplit splits are done (optional) |
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.
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.
Here is an example of how the error occurs.
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.
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.The most common source of a None
value (other than an explicit assignment) is
a function that doesn't return anything.
# ๐๏ธ 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
.
get()
which caused the error.The error occurs for multiple reasons:
None
implicitly).None
.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.
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()
.
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.
Another common cause of the error is having a function that returns a value only if a condition is met.
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
.
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.
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.
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.
Here is a very simple example of how the error occurs.
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.
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.The most common source of a None
value (other than an explicit assignment) is
a function that doesn't return anything.
# ๐๏ธ 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
.
lower
which caused the error.The error occurs for multiple reasons:
None
implicitly).None
.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()
.
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.
Another common cause of the error is having a function that returns a value only if a condition is met.
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
.
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.
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 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.
Here is a very simple example of how the error occurs.
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.
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.The most common source of a None
value (other than an explicit assignment) is
a function that doesn't return anything.
# ๐๏ธ 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
.
replace()
which caused the error.The error occurs for multiple reasons:
None
implicitly).None
.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()
.
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.
Another common cause of the error is having a function that returns a value only if a condition is met.
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
.
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.
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 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.
Here is a very simple example of how the error occurs.
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.
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.The most common source of a None
value (other than an explicit assignment) is
a function that doesn't return anything.
# ๐๏ธ 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
.
encode
which caused the error.The error occurs for multiple reasons:
None
implicitly).None
.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()
.
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.
Another common cause of the error is having a function that returns a value only if a condition is met.
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
.
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.
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 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.
Here is an example of how the error occurs.
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.
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.The most common source of a None
value (other than an explicit assignment) is
a function that doesn't return anything.
# ๐๏ธ 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
.
items()
which caused the error.The error occurs for multiple reasons:
None
implicitly).None
.None
.Make sure you aren't assigning the result of calling a method that returns
None
to a variable.
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()
.
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.
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
.
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.
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.
You can learn more about the related topics by checking out the following tutorials: