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

avatar
Borislav Hadzhiev

Last updated: Apr 8, 2024
3 min

banner

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

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.

attributeerror nonetype object has no attribute group

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

main.py
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.

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.

For example, the re.match() and re.search() methods return None if there are no matches.

main.py
import re # ๐Ÿ‘‡๏ธ None m = re.match(r"(\w+) (\w+)", "hello") # โ›”๏ธ AttributeError: 'NoneType' object has no attribute 'group' print(m.group())

function returning none

Since the string does not match the pattern, the match() method returns None.

# Using a try/except statement to avoid getting the error

One way to solve the error is to use a try/except statement.

main.py
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

using try except to avoid the error

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.

# Check if the value is not None before calling group()

You can get around this by using an if statement to check if there are matches before calling group().

main.py
import re m = re.match(r"(\w+) (\w+)", "hello") if m: print(m.group()) else: print('There are no matches') # ๐Ÿ‘‰๏ธ this runs

check if value is not none before calling group

You could also be more explicit in the if statement and check that the m variable is not None.

main.py
import re m = re.match(r"(\w+) (\w+)", "hello") if m is not None: print(m.group()) else: print('There are no matches')

# The re.search() method possibly returns None

The re.search() method also returns None if no position in the string matches the pattern.

main.py
import re # ๐Ÿ‘‡๏ธ None m = re.search('(?<=abc)def', 'xyz') # โ›”๏ธ AttributeError: 'NoneType' object has no attribute 'group' print(m.group(0))

re search method possibly returns none

You can use the same approach to only call the group method if there was a match.

main.py
import re m = re.search('(?<=abc)def', 'abcdef') if m is not None: print(m.group()) # ๐Ÿ‘‰๏ธ def else: print('There are no matches')

only call group method if there is a match

The if block is only run if the m variable is not None, otherwise the else block runs.

# The Match.groups() method possibly returns None

The same is the case when using the Match.groups method.

main.py
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.

main.py
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:

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