Jacob Griffin
Jacob Griffin

Reputation: 5169

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

I got this error:

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

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.

Upvotes: 486

Views: 2092466

Answers (11)

jmx-jiang
jmx-jiang

Reputation: 69

The error means that you are trying to access an attribute or method of None, but NoneType does not have any. This can happen when you call a function that does not return anything, which results in its return value is None. Example:

>>> exec("'FOO'").lower()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'lower'

This happens because the built-in function exec does not return a value, and we are trying to call the lower method on None.

Upvotes: 1

tripleee
tripleee

Reputation: 189789

Another common cause of getting None is to incorrectly assign a method's return value to the manipulated object.

numbers = [2, 3, 4, 0, 42, 17]
numbers = numbers.sort()  # error!
numbers.append(10000000)

The problem here is that the .sort method modifies the list in place, and returns None. You want either

numbers.sort()

or

numbers = numbers.sorted()

(The former should probably be preferred in this scenario.)

There are several other methods with a similar signature. sort is a common one, but it's just one example of this.


There was a moderately highly upvoted answer with this information, but its author deleted it. I'm adding a new answer to keep the discussion reasonably complete.

Upvotes: 3

g.d.d.c
g.d.d.c

Reputation: 48028

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.

Upvotes: 460

David Newcomb
David Newcomb

Reputation: 10943

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')

Upvotes: 0

Shah Vipul
Shah Vipul

Reputation: 747

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.

Upvotes: 8

koblas
koblas

Reputation: 27098

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'

Upvotes: 145

Chiel
Chiel

Reputation: 2179

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.

Upvotes: 2

barribow
barribow

Reputation: 111

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.

Upvotes: 0

M. Hamza Rajput
M. Hamza Rajput

Reputation: 10296

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")

Upvotes: 3

PHINCY L PIOUS
PHINCY L PIOUS

Reputation: 373

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.

Upvotes: 15

S.Lott
S.Lott

Reputation: 391972

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.

Upvotes: 19

Related Questions