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

前端 未结 10 2036
长情又很酷
长情又很酷 2020-11-21 13:36

I keep getting an error that says

AttributeError: \'NoneType\' object has no attribute \'something\'
相关标签:
10条回答
  • 2020-11-21 13:50

    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.

    0 讨论(0)
  • 2020-11-21 13:52

    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")
    
    0 讨论(0)
  • 2020-11-21 13:52

    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.

    0 讨论(0)
  • 2020-11-21 13:53

    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.

    0 讨论(0)
  • 2020-11-21 13:53

    Others have explained what NoneType is and a common way of ending up with it (i.e., failure to return a value from a function).

    Another common reason you have None where you don't expect it is assignment of an in-place operation on a mutable object. For example:

    mylist = mylist.sort()
    

    The sort() method of a list sorts the list in-place, that is, mylist is modified. But the actual return value of the method is None and not the list sorted. So you've just assigned None to mylist. If you next try to do, say, mylist.append(1) Python will give you this error.

    0 讨论(0)
  • 2020-11-21 13:53

    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.
    0 讨论(0)
提交回复
热议问题