Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I get AttributeError: 'NoneType' object has no attribute 'something'?

I keep getting an error that says

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

The code I have is too long to post here. What general scenarios would cause this AttributeError, what is NoneType supposed to mean and how can I narrow down what's going on?

like image 400
Jacob Griffin Avatar asked Jan 20 '12 23:01

Jacob Griffin


People also ask

What does NoneType object has no attribute mean?

You are getting AttributeError: 'NoneType' object has no attribute 'something' because NoneType means that instead of an instance of whatever Class or Object you think you're working with, you've actually got None. It means that an assignment or function call up above failed or returned an unexpected result.

Why am I getting AttributeError object has no attribute?

If you are getting an object that has no attribute error then the reason behind it is because your indentation is goofed, and you've mixed tabs and spaces.

How do I fix NoneType errors in Python?

The error “TypeError: 'NoneType' object is not iterable” occurs when you try to iterate over a NoneType object. Objects like list, tuple, and string are iterables, but not None. To solve this error, ensure you assign any values you want to iterate over to an iterable object.


2 Answers

NoneType means that instead of an instance of whatever Class or Object you think you're working with, you've actually got None. That usually means that an assignment or function call up above failed or returned an unexpected result.

like image 199
g.d.d.c Avatar answered Sep 18 '22 14:09

g.d.d.c


You have a variable that is equal to None and you're attempting to access an attribute of it called 'something'.

foo = None foo.something = 1 

or

foo = None print(foo.something) 

Both will yield an AttributeError: 'NoneType'

like image 41
koblas Avatar answered Sep 22 '22 14:09

koblas