AttributeError happens when an object exists but does not expose the attribute or method you asked for. In Python, that often means a typo, a wrong object type, or a value that turned into None earlier in the flow.
The topic is best explained by showing how attribute lookup works, why hasattr and getattr help during debugging, and why NoneType errors usually point to a missing return or failed lookup upstream.
A strong page should show the exact object state that caused the miss, the attribute name Python looked for, and the quickest way to confirm whether the object is the right kind before the access.
An AttributeError occurs in Python when you try to access or call an attribute or method that does not exist on an object. This commonly happens when a function returns None instead of the expected object, when there is a typo in the attribute name, or when you use the wrong object type. Python raises this error at runtime when the attribute lookup fails.
# Bad return path: function gives back None
def get_text():
text = "hello world"
# forgot return
result = get_text()
words = result.split() # AttributeError: 'NoneType' object has no attribute 'split'
# Fixed return path: return the text object
def get_text():
text = "hello world"
return text # Always return the value
result = get_text()
words = result.split() # ['hello', 'world']
The most common cause of AttributeError: 'NoneType' object has no attribute is calling a method on a variable that holds None. This usually happens when a function is missing a return statement.
Python attribute names are case-sensitive. A single character difference - like append vs Append - will cause an AttributeError. Always check the exact spelling of methods in the documentation.
Calling a method that belongs to one type on a different type raises an AttributeError. For example, calling a dictionary method on a list, or a string method on an integer.
If you forget to import a module and try to use its attributes, Python raises an AttributeError (or NameError). Always import modules at the top of your file before using them.
my_list = [1, 2, 3]
sorted_list = my_list.sort() # sort() returns None, not the sorted list!
print(sorted_list[0]) # AttributeError: 'NoneType' object has no attribute '__getitem__'
my_list = [1, 2, 3]
my_list.sort() # sort() modifies in-place, returns None
print(my_list[0]) # 1
# Or use sorted() which returns a new list
sorted_list = sorted(my_list) # Returns a new sorted list
print(sorted_list[0]) # 1
name = "hello world"
print(name.Split()) # AttributeError: 'str' object has no attribute 'Split'
print(name.lenght) # AttributeError: 'str' object has no attribute 'lenght'
name = "hello world"
print(name.split()) # lowercase 'split'
print(len(name)) # len() is a built-in function, not an attribute
# Use dir() to see all available attributes
print(dir(name)) # Lists all string methods
data = [1, 2, 3]
data.keys() # AttributeError: 'list' object has no attribute 'keys'
number = 42
number.upper() # AttributeError: 'int' object has no attribute 'upper'
data = {"a": 1, "b": 2} # Use a dict if you need .keys()
data.keys() # dict_keys(['a', 'b'])
number = 42
str(number).upper() # Convert to string first: "42"
# Check type before calling methods
if isinstance(data, dict):
print(data.keys())
result = math.sqrt(16) # NameError: name 'math' is not defined
# (or AttributeError if math was partially imported)
import math # Import at the top of the file
result = math.sqrt(16) # 4.0
print(result)
It means you tried to access an attribute or method that does not exist on the object. Either the object is the wrong type, the name is misspelled, or the object is None.
This means the variable holds None instead of the expected object. Check if the function that produced the value has a return statement, or if an API call returned None.
Use <code>hasattr(obj, 'attribute_name')</code> which returns True or False. Alternatively, use <code>getattr(obj, 'attr', default)</code> to get a default value if the attribute does not exist.
Explore 500+ free tutorials across 20+ languages and frameworks.