Skip to content
How to Fix AttributeError in Python

How to Fix AttributeError in Python

DodaTech Updated Jun 15, 2026 2 min read

Error message: AttributeError: 'NoneType' object has no attribute 'something'

Python raises AttributeError when you try to access an attribute or method that doesn’t exist on an object. It’s the second most common runtime error after TypeError.

Cause 1: Typo in Method or Attribute Name

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

    def greet(self):
        return f"Hello, {self.name}"

p = Person("Alice")
print(p.greet())   # OK — "Hello, Alice"
print(p.gret())    # AttributeError: 'Person' object has no attribute 'gret'

Fix: Check spelling. Common typos: lenghtlength, uperupper, conatinscontains.

Cause 2: Variable Is None When You Expect an Object

This is the most common AttributeError:

data = {"user": None}
print(data["user"].upper())  # AttributeError: 'NoneType' object has no attribute 'upper'

Fix: Check for None before accessing attributes:

user = data.get("user")
if user is not None:
    print(user.upper())
else:
    print("No user found")

Cause 3: Missing __init__ Method

If your class doesn’t initialize an attribute, it won’t exist:

class Book:
    pass  # No __init__!

b = Book()
b.title = "1984"  # OK — dynamic attribute
print(b.author)    # AttributeError: 'Book' object has no attribute 'author'

Fix: Always define attributes in __init__:

class Book:
    def __init__(self, title="", author=""):
        self.title = title
        self.author = author

Cause 4: Method Name Conflicts with Attribute

class Circle:
    def __init__(self, radius):
        self.radius = radius

    @property
    def area(self):
        return 3.14 * self.radius ** 2

c = Circle(5)
print(c.area)    # Works — @property called without ()
c.area = 10      # AttributeError: can't set attribute (it's a property)

Fix: If you need to set it, add a setter or use a different name.

Cause 5: List Index Instead of Method Call

items = [1, 2, 3]
items.append(4)    # OK
items.apppend(5)   # AttributeError: 'list' object has no attribute 'apppend'

# Another common one
items.sort()       # OK
items.sorted()     # AttributeError: 'list' object has no attribute 'sorted'
# sorted() is a built-in function, not a list method

Prevention

  • Use an IDE or editor with autocompletion — it catches typos immediately
  • Check that functions return a value, not None implicitly
  • Initialize all attributes in __init__
  • Use hasattr(obj, "attr_name") before accessing if unsure
  • Use getattr(obj, "attr_name", default) with a safe default
  • When chaining method calls, check each intermediate result for None

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro