← Back to Error Guide
AttributeError

How to Fix AttributeError

Understanding and resolving AttributeError in Python

What is AttributeError?

An AttributeError is raised when you try to use an attribute or method that an object does not have.

Common Causes

Common causes of this error...

How to Fix

Wrong Code

# Code that causes the error
class Dog:
    def __init__(self, name):
        self.name = name

my_dog = Dog('Buddy')
print(my_dog.age)

Correct Code

# Fixed code
class Dog:
    def __init__(self, name, age):
        self.name = name
        self.age = age

my_dog = Dog('Buddy', 5)
print(my_dog.age)

More Python Error Guides