AttributeError

obj.x where obj has no x. Read the type in the message first — 'NoneType' object has no attribute means the bug is wherever that None came from, not the attribute.

InheritsBaseException›Exception›AttributeError
Lookup exceptionPython 3 (all)Live demo
AttributeError(*args, namename — Keyword-only. The attribute that was looked up. Filled in automatically for failed lookups (3.10+).type: str | None · default: None=None, objobj — Keyword-only. The object the lookup was made on (3.10+).type: object · default: None=None)
Raised by
obj.attr, obj.attr = v, del obj.attr, getattr(obj, name)
Message
'<type>' object has no attribute '<name>' · module 'm' has no attribute 'x'
Quick fix
getattr(obj, name, default) · check for None first
Watch out
"Did you mean" is added by the traceback, not part of str(e)

Demo

Live evaluation
Look up an attribute by name on an instance with two attributes, name and email. Try a typo.
Try:
Inputs
attrstrattribute name
Code
class User:
    def __init__(self):
        self.name = 'Ada'
        self.email = 'ada@example.com'

u = User()
getattr(u, 'email')
Result
'ada@example.com'

In Trigger, a close typo gets . Did you mean: 'email'? on the traceback line, while age (nothing similar) gets none. That suggestion is computed by the traceback printer — f'{e}' in Handle shows the plain str(e), which never contains it. In Handle, the error names NoneType, not re.Match: the real problem is that match() found nothing. Checking if m is None is usually clearer than catching the exception.

Constructor

NameTypeRequiredDescription
*argsobjectnoUsually one message string. str(e) is that message.
namestr | Noneno (None)Keyword-only. The attribute that was looked up. Filled in automatically for failed lookups (3.10+).
objobjectno (None)Keyword-only. The object the lookup was made on (3.10+).

Attributes

AttributeTypeMeaning
namestr | NoneThe attribute name that was not found (3.10+). Set automatically by failed attribute lookups, including getattr().
objobjectThe object the attribute was looked up on (3.10+) — for the NoneType case, None itself.
argstupleThe constructor arguments; args[0] is the message string.

Common patterns

Optional attribute with a default
getattr with a third argument returns the default instead of raising.
timeout = getattr(config, 'timeout', 30)
Guard against None before the call
Most 'NoneType' object has no attribute errors come from a function that returned None. Check at the source.
m = re.match(pattern, text)
if m is None:
    raise ValueError(f'unexpected format: {text!r}')
value = m.group(1)
Dynamic attributes via __getattr__
__getattr__ is only called when normal lookup fails; raise AttributeError (not KeyError) for unknown names so getattr defaults and hasattr keep working.
class Settings:
    def __init__(self, data):
        self._data = data
    def __getattr__(self, attr):
        try:
            return self._data[attr]
        except KeyError:
            raise AttributeError(f'no setting named {attr!r}') from None
Feature detection
hasattr() calls getattr() and returns False on AttributeError.
if hasattr(stream, 'fileno'):
    fd = stream.fileno()

Examples

1. Method on None
None.upper()
Returns
AttributeError: 'NoneType' object has no attribute 'upper'
2. list.sort() returns None
nums = [3, 1, 2] nums = nums.sort() nums.append(4)
Returns
AttributeError: 'NoneType' object has no attribute 'append'
3. Module attribute typo
import json json.laods('[]')
Returns
AttributeError: module 'json' has no attribute 'laods'. Did you mean: 'loads'?
4. Method from another language
[].push(1)
Returns
AttributeError: 'list' object has no attribute 'push'
5. Dict keys are not attributes
d = {'name': 'Ada'} d.name
Returns
AttributeError: 'dict' object has no attribute 'name'
6. On a class, not an instance
class C: pass C.x
Returns
AttributeError: type object 'C' has no attribute 'x'
7. e.name and e.obj
try: 'text'.foo except AttributeError as e: r = (e.name, e.obj) r
Returns
('foo', 'text')
8. str(e) has no suggestion
try: [].appendd except AttributeError as e: r = str(e) r
Returns
"'list' object has no attribute 'appendd'"

Pitfalls

1. Assigning the result of an in-place method
list.sort(), list.append(), dict.update() and friends mutate in place and return None. Reassigning turns the variable into None.
nums = nums.sort()
nums = [3, 1, 2]
nums = nums.sort()
nums.append(4)
AttributeError: 'NoneType' object has no attribute 'append'
nums.sort()
nums = [3, 1, 2]
nums.sort()
nums.append(4)
nums
[1, 2, 3, 4]
2. Using the match object without checking it
re.match/re.search return None when nothing matches, so .group() fails with a NoneType message far from the real cause.
m.group()
import re
m = re.match(r'[0-9]+', 'abc')
m.group()
AttributeError: 'NoneType' object has no attribute 'group'
check m first
import re
m = re.match(r'[0-9]+', 'abc')
n = m.group() if m else None
n is None
True
3. An AttributeError inside a property looks like a missing property
A bug inside a @property getter raises AttributeError for the inner name, and __getattr__ fallbacks or hasattr() will silently treat it as "attribute missing".
hasattr hides the bug
class A:
    @property
    def total(self):
        return self.itmes  # typo
hasattr(A(), 'total')
False
Access it directly
class A:
    @property
    def total(self):
        return self.itmes  # typo
A().total
AttributeError: 'A' object has no attribute 'itmes'
4. Slots block new attributes
A class with __slots__ (and plain object()) has no per-instance __dict__, so assigning an undeclared name fails.
Undeclared name
class P:
    __slots__ = ('x',)
p = P()
p.y = 1
AttributeError: 'P' object has no attribute 'y' and no __dict__ for setting new attributes
Declare it
class P:
    __slots__ = ('x', 'y')
p = P()
p.y = 1
p.y
1

When to use

Use it
  • Raising it from __getattr__ / __getattribute__ for unknown names
  • Catching it for duck typing when an optional method may be missing
  • Read-only attributes: raise it from a property setter or __setattr__
Reach for something else
  • Optional attribute → getattr(obj, name, default)
  • Value may be None → test is None before using it
  • Wrapping large blocks in except AttributeError — it hides typos

Notes

Did you mean
Added by the traceback formatter (traceback.TracebackException), using e.name and e.obj — not stored in the exception. Custom sys.excepthook or logging with str(e) will not show it.
Message shapes
'T' object has no attribute 'x' (instance), type object 'T' has no attribute 'x' (class), module 'm' has no attribute 'x' (module)
Read-only
Built-in types reject assignment: 'str' object attribute 'upper' is read-only
hasattr
hasattr(obj, name) is getattr() plus except AttributeError — any AttributeError raised inside a property counts as missing

FAQ

The variable you called the method on is None. Typical sources: a function without a return statement, an in-place method whose result was assigned (x = x.sort()), re.match() that found nothing, or dict.get() on a missing key. Fix the place that produced None, or check is None before using it.

History

3.10
Added the name and obj attributes.