Last updated: Apr 8, 2024
Reading timeยท3 min
The Python "AttributeError: 'NoneType' object has no attribute" occurs when we
try to call the group()
method on a None
value, e.g. after calling match()
or search()
with no matches.
To solve the error, use an if
statement before calling group
.
Here is a very simple example of how the error occurs.
example = None # โ๏ธ AttributeError: 'NoneType' object has no attribute 'group' example.group()
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.The most common source of a None
value (other than an explicit assignment) is
a function that doesn't return anything.
For example, the
re.match() and
re.search() methods return None
if there are no matches.
import re # ๐๏ธ None m = re.match(r"(\w+) (\w+)", "hello") # โ๏ธ AttributeError: 'NoneType' object has no attribute 'group' print(m.group())
Since the string does not match the pattern, the match()
method returns
None
.
try/except
statement to avoid getting the errorOne way to solve the error is to use a try/except statement.
import re a_string = 'bobby hadz com' try: matches = re.match(r"(\w+) (\w+)", a_string).group() except AttributeError: matches = re.match(r"(\w+) (\w+)", a_string) print(matches) # ๐๏ธ bobby hadz
If the string doesn't match the pattern, the re.match()
method
returns None and accessing the group()
method on
a None
value causes an AttributeError
exception.
In this case, the except
block runs where we don't call the group()
method.
You can get around this by using an if
statement to check if there are matches
before calling group()
.
import re m = re.match(r"(\w+) (\w+)", "hello") if m: print(m.group()) else: print('There are no matches') # ๐๏ธ this runs
You could also be more explicit in the if
statement and check that the m
variable is not None
.
import re m = re.match(r"(\w+) (\w+)", "hello") if m is not None: print(m.group()) else: print('There are no matches')
re.search()
method possibly returns NoneThe re.search()
method also returns None
if no position in the string
matches the pattern.
import re # ๐๏ธ None m = re.search('(?<=abc)def', 'xyz') # โ๏ธ AttributeError: 'NoneType' object has no attribute 'group' print(m.group(0))
You can use the same approach to only call the group
method if there was a
match.
import re m = re.search('(?<=abc)def', 'abcdef') if m is not None: print(m.group()) # ๐๏ธ def else: print('There are no matches')
The if
block is only run if the m
variable is not None
, otherwise the
else
block runs.
Match.groups()
method possibly returns NoneThe same is the case when using the Match.groups method.
import re # ๐๏ธ none m = re.match(r"(\d+)\.(\d+)", "hello") # โ๏ธ AttributeError: 'NoneType' object has no attribute 'groups' print(m.groups())
The Match.groups
method returns None
if there are no matches and a default
argument is not provided.
To get around the error, use a simple if
statement.
import re # ๐๏ธ none m = re.match(r"(\d+)\.(\d+)", "123.456") if m is not None: print(m.groups()) # ๐๏ธ ('123', '456') else: print('There are no matches')
The "AttributeError: 'NoneType' object has no attribute 'group'" occurs for multiple reasons:
None
implicitly).None
.