Skip to content

AttributeError: 'x' object has no attribute 'y'

Error message

                'x' object has no attribute 'y'
              

Quick summary

You tried to use a method or property on an object that doesn't have it — usually a typo or the wrong object type.

Why this happens

  • Typo in the method or attribute name

  • The object is the wrong type (e.g., calling .append() on a string)

  • The object is None and you accessed an attribute on it

Minimal example

✗ Broken code
name = "Alice"
name.append("x")
✓ Fixed code
name = ["Alice"]
name.append("x")

Strings don't have an .append() method — use a list instead if you need to add elements.

How to diagnose

  • Check the method name with dir(obj) to see available attributes

  • Verify the object type with type(obj)

  • Read the error — it names the type and the missing attribute

How to fix

  • Fix the method or attribute name typo

  • Convert the object to the correct type before calling the method

  • Use hasattr(obj, 'attr') to check before accessing

How to prevent

  • Use an IDE with autocomplete to avoid method name typos

  • Use type hints to catch type mismatches before runtime

Related Resources

Related Glossary

Related Lessons

  • Data Types

    Learn about Python data types and their methods

Related Practice

← Back to language