Skip to content
AttributeError: '...' object has no attribute '...'

AttributeError: '...' object has no attribute '...'

DodaTech 2 min read

AttributeError: '...' object has no attribute '...' means you tried to access an attribute or method on an object that doesn’t have it. Python raises this when dot-notation fails.

What It Means

Every Python object has a set of attributes (variables) and methods (functions) attached to it. When you write obj.something, Python looks for something on the object. If it’s not there — AttributeError.

Why It Happens

  • Typo in the attribute/method name (e.g. length instead of len)
  • The object is a different type than you expected (e.g. None instead of a string)
  • Method vs property confusion (e.g. calling .items instead of .items() on a dict)
  • The attribute exists but is prefixed with underscore (name mangling)
  • Forgot to import or instantiate properly
  • A function returned None and you chain a method call on the result

How to Fix It

Step 1: Check the type of the object

data = None

# BAD
print(data.append(1))  # AttributeError: 'NoneType' object has no attribute 'append'

# GOOD — verify the type first
if data is not None:
    print(data.append(1))
else:
    print("data is None, cannot append")

# Or use isinstance
if isinstance(data, list):
    print(data.append(1))

Step 2: Fix typos and case sensitivity

text = "Hello, World!"

# BAD — Python is case-sensitive
print(text.Upper())

# GOOD
print(text.upper())

Step 3: Methods need parentheses

data = {"name": "Alice", "age": 30}

# BAD — .items is a method, returns a bound method object
print(data.items)

# GOOD — call the method with ()
print(data.items())

Step 4: Chain safely after function calls

# BAD — get() might return None, and None has no .upper()
name = data.get("name").upper()

# GOOD — use a default
name = data.get("name", "").upper()

# Or check in steps
name = data.get("name")
if name:
    name = name.upper()

Step 5: Use hasattr() or getattr() for dynamic access

obj = SomeClass()

# Check before accessing
if hasattr(obj, "some_method"):
    obj.some_method()

# Or use getattr with a default
method = getattr(obj, "some_method", None)
if method:
    method()
How do I see all available attributes of an object?
Use dir(obj) in a Python REPL to list all attributes and methods. For a cleaner view, filter out dunder methods: [a for a in dir(obj) if not a.startswith('__')].
Why do I get AttributeError on a variable I just defined?
The variable might be None or a different type than you think. Add print(type(my_var)) before the failing line to see what Python actually stored. This is especially common with functions that return None implicitly when you expect them to return a value.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro