fuckingpython.lol

Python errors › AttributeError

AttributeError: 'NoneType' object has no attribute

Updated

The Python error "'NoneType' object has no attribute" means a call or lookup gave you None instead of an object. The usual sources, and the fix.

What you see

main.py
import re

line = "total: n/a"
match = re.search(r"\d+", line)
print(match.group())
Python 3.15 prints
Traceback (most recent call last):
  File "/home/me/project/main.py", line 5, in <module>
    print(match.group())
          ^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'group'

What it means

You wrote something.name and something was None. The message names the attribute you asked for, 'group' here, not the variable. Look for the dot just before that name.

None is the only value of the type NoneType, which is why the message says 'NoneType'. It has almost no attributes, so nearly any method call on it fails this way.

Why it happens

  • A search that found nothing. re.match(), re.search() and re.fullmatch() return None when the pattern does not match. BeautifulSoup's find() does the same when no tag matches.
  • Chaining on an in-place method. items.sort().reverse() calls .reverse() on the None that sort() returned.
  • A function without a return on the path that ran.
  • An attribute that was never set. self.conn = None in __init__, and connect() not called before self.conn.cursor().

How to fix it

Handle the "nothing found" case before you use the result:

match = re.search(r"\d+", line)
if match:
    print(match.group())
else:
    print("no number in", repr(line))

For in-place methods, put each call on its own line: items.sort(), then items.reverse().

How each Python version words it

PythonPrints
3.9–3.15AttributeError: 'NoneType' object has no attribute 'group'

Run on CPython 3.9.6, 3.10.22, 3.11.17, 3.12.15, 3.13.17, 3.14.9 and 3.15.1, on October 25, 2026.

The wording is the same from 3.9 to 3.15. From 3.10 the interpreter adds "Did you mean" suggestions when another attribute's name is close to the one you typed, but None has no attribute close to group, so none appears here.

Questions

Is if match: safe, or should I write is not None?

For match objects both work: the re documentation says a match object is always true. For values that can be legitimately false, such as 0, "" or an empty list, write is not None.

How do I find which call returned None?

Split the chain. soup.find("div").find("a").text becomes three lines, each printed or checked. The traceback's caret in 3.11 and later also underlines the part of the line that failed.

Sources

Read on October 25, 2026.