449

I am getting an error message that says

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

How can I understand this message?

What general scenarios might cause such an AttributeError, and how can I identify the problem?


This is a special case of AttributeErrors. It merits separate treatment because there are a lot of ways to get an unexpected None value from the code, so it's typically a different problem; for other AttributeErrors, the problem might just as easily be the attribute name.

See also What is a None value? and What is a 'NoneType' object? for an understanding of None and its type, NoneType.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Jacob Griffin
  • 4,807
  • 3
  • 16
  • 11
  • 2
    TODO: find a **proper** canonical for the general case of `AttributeError`, to include in the see-also section. https://stackoverflow.com/questions/11685936 is **not suitable**, because it was caused by a typo (mixed tab/space indentation) that has 2.x-specific effects, which causes confusion and inaccuracy in the answers. – Karl Knechtel Mar 26 '23 at 05:21

10 Answers10

432

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.

g.d.d.c
  • 46,865
  • 9
  • 101
  • 111
  • 3
    Remembering to actually *return* from a function call is also helpful... trying to reference an item that wasn't returned doesn't usually work too well. Silly me. – Shrout1 Oct 27 '22 at 18:06
  • This was the exact issue for me. I keep coming back here often. :D Thanks. – rahulspoudel Jan 22 '23 at 10:24
137

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'

Błażej Michalik
  • 4,474
  • 40
  • 55
koblas
  • 25,410
  • 6
  • 39
  • 49
  • 21
    This is probably unhelpful until you point out how people might end up getting a `None` out of something. An explicit `foo = None` is unlikely to be the problem; it's going to be `foo = something()` and you don't realize `something()` might return `None` when it doesn't succeed or the result set was empty or whatever. – tripleee Feb 04 '19 at 08:02
  • This is totally correct. For instance when you are using Django to develop an e-commerce application, you have worked on functionality of the cart and everything seems working when you test the cart functionality with a product. Then in the backend you delete the product been registered to the cart. If you attempt to go to the cart page again you will experience the error above. Proper fix must be handled to avoid this. – Adeolu Temitope Olofintuyi Aug 27 '21 at 07:42
17

The NoneType is the type of the value None. In this case, the variable lifetime has a value of None.

A common way to have this happen is to call a function missing a return.

There are an infinite number of other ways to set a variable to None, however.

S.Lott
  • 384,516
  • 81
  • 508
  • 779
  • 1
    I don't think `lifetime` had a value of `None` (pre-edit). He was trying to access the lifetime _attribute_ of something else that was `None`. – g.d.d.c Nov 14 '14 at 03:09
15

Consider the code below.

def return_something(someint):
 if  someint > 5:
    return someint

y = return_something(2)
y.real()

This is going to give you the error

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

So points are as below.

  1. In the code, a function or class method is not returning anything or returning the None
  2. Then you try to access an attribute of that returned object(which is None), causing the error message.
PHINCY L PIOUS
  • 335
  • 1
  • 3
  • 7
6
if val is not None:
    print(val)
else:
    # no need for else: really if it doesn't contain anything useful
    pass

Check whether particular data is not empty or null.

tripleee
  • 175,061
  • 34
  • 275
  • 318
Shah Vipul
  • 625
  • 7
  • 11
  • 1
    `else: pass` is completely useless; if you don't have anything to put in `else:`, simply omit it. – tripleee Nov 06 '22 at 08:49
4

It means the object you are trying to access None. None is a Null variable in python. This type of error is occure de to your code is something like this.

x1 = None
print(x1.something)

#or

x1 = None
x1.someother = "Hellow world"

#or
x1 = None
x1.some_func()

# you can avoid some of these error by adding this kind of check
if(x1 is not None):
    ... Do something here
else:
    print("X1 variable is Null or None")
M. Hamza Rajput
  • 7,810
  • 2
  • 41
  • 36
3

When building a estimator (sklearn), if you forget to return self in the fit function, you get the same error.

class ImputeLags(BaseEstimator, TransformerMixin):
    def __init__(self, columns):
        self.columns = columns

    def fit(self, x, y=None):
        """ do something """

    def transfrom(self, x):
        return x

AttributeError: 'NoneType' object has no attribute 'transform'?

Adding return self to the fit function fixes the error.

Chiel
  • 1,865
  • 1
  • 11
  • 24
  • 1
    Perhaps it's worth pointing out that functions which do not explicitly `return` anything will implicitly `return None`. This is also the case if your function is basically `if something: return value` which will then end up in the implicit `return None` limbo when `something` is not truthy. (The answer by PHINCY L PIOUS elaborates on this particular case.) – tripleee Apr 07 '22 at 04:52
1

g.d.d.c. is right, but adding a very frequent example:

You might call this function in a recursive form. In that case, you might end up at null pointer or NoneType. In that case, you can get this error. So before accessing an attribute of that parameter check if it's not NoneType.

Prashant Sengar
  • 506
  • 1
  • 7
  • 24
barribow
  • 109
  • 2
  • 4
  • Yeah, ok, but how do you do that check? – not2qubit Oct 15 '19 at 17:37
  • `if foo is not None:` – Gringo Suave Nov 05 '19 at 21:01
  • It is true that forgetting to `return` something in every scenario is a common bug for recursive functions in particular; but the exposition is this answer does not seem detailed enough to be actually useful. See the common FAQ https://stackoverflow.com/questions/17778372/why-does-my-recursive-function-return-none – tripleee May 06 '23 at 10:02
0

You can get this error with you have commented out HTML in a Flask application. Here the value for qual.date_expiry is None:

   <!-- <td>{{ qual.date_expiry.date() }}</td> -->

Delete the line or fix it up:

<td>{% if qual.date_attained != None %} {{ qual.date_attained.date() }} {% endif %} </td>
Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
-1

None of the other answers here gave me the correct solution. I had this scenario:

def my_method():
   if condition == 'whatever':
      ....
      return 'something'
   else:
      return None

answer = my_method()

if answer == None:
   print('Empty')
else:
   print('Not empty')

Which errored with:

File "/usr/local/lib/python3.9/site-packages/gitlab/base.py", line 105, in __eq__
if self.get_id() and other.get_id():
AttributeError: 'NoneType' object has no attribute 'get_id'

In this case you can't test equality to None with ==. To fix it I changed it to use is instead:

if answer is None:
   print('Empty')
else:
   print('Not empty')
tripleee
  • 175,061
  • 34
  • 275
  • 318
David Newcomb
  • 10,639
  • 3
  • 49
  • 62
  • 1
    One of the lessons is to think hard about when `return None` creates more problems than it solves. In some cases, raising an exception might make more sense. – tripleee Nov 06 '22 at 08:49