HOWTO · Python

How to Fix Object Has No Attribute Error in Python

This tutorial discusses the object has no attribute error in Python.

On this page

Python raises AttributeError when an attribute lookup fails on the object before the dot. The requested name may be misspelled, defined on a different type, or absent because the object was initialized differently than expected. For example, a list and a NumPy array are different types, so an attribute available on one is not automatically available on the other.

What the Python “object has no attribute” error means

In items.size, Python asks the object bound to items for an attribute named size. The error identifies that object and the missing name; it does not mean that the name is unavailable everywhere in Python. Check the type of the object receiving the access before changing the code.

Find why the attribute is missing

Start where the failing expression runs. Print or inspect the variable’s type, then check spelling and capitalization against the class or API you intended to use. A value may also have been replaced earlier with a different object, such as a list in place of an array.

For a quick inspection, dir(value) lists many names available on an object. It is a debugging aid, not a complete proof that an attribute cannot be accessed: metaclass behavior can expose attributes that dir() does not show. See the Python metaclass guide for related details.

A small class example makes the receiver clear. User defines name, while the unrelated Guest class does not:

class User:
    def __init__(self, name):
        self.name = name


class Guest:
    pass


person = User("Ada")
visitor = Guest()
print(person.name)
print(type(visitor).__name__)
try:
    visitor.name
except AttributeError:
    print("visitor has no name")

The first access succeeds because person is a User with a name. The final access asks the Guest instance visitor for name; the narrowly scoped handler catches that lookup’s AttributeError and prints a stable explanation. Methods defined on instances must include self as their first parameter so Python can pass the instance when they are called.

Fix the attribute error

Match the fix to the cause: correct a typo, call the attribute on the intended object, or define and initialize the attribute or method on the class that should provide it. Do not change an object’s type merely to make a name available; other code may depend on its original type and fail after the change.

For a normal successful access, keep the class and the object’s data together:

Run this example:

class Account:
    def __init__(self, owner):
        self.owner = owner


account = Account("Mina")
print(account.owner)
Mina

Handle optional attributes safely

When an attribute is genuinely optional, hasattr(value, "name") checks whether lookup succeeds. Use it as a guard when the two paths need different behavior. For a fallback value, getattr(value, "name", default) returns the default if that lookup raises AttributeError. setattr() assigns an attribute; it does not provide a fallback during lookup.

If handling the error directly is clearer, put only the uncertain access inside a try block and catch AttributeError there. This keeps unrelated failures visible. Choose the fallback based on what the rest of the program can safely do; a placeholder is appropriate only when it has the same meaning as a missing value for your task.